在XSL中包含带有Curly Brackets {}的内联JavaScript [英] Include Inline JavaScript with Curly Brackets {} in XSL

查看:130
本文介绍了在XSL中包含带有Curly Brackets {}的内联JavaScript的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am attempting to transform XML to HTML via an XSL file. Unfortunately, it does not allow me to use JavaScript curly Brackets {}. The following is a trivial example, but my actual code is much larger.

<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>

Visual Studio给出了以下错误:

Visual Studio gives me the following error:

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

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

Is there any way to include inline JavaScript in XSL? I know that you can use <![CDATA[ ]]> to escape Javascript blocks. But how do I escape inline JavaScript? My actual code is too large to re-write all inline JavaScript as script blocks.

推荐答案

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

Curly braces in attributes of literal result elements are used for "attribute value templates", enclosing XPath expressions. So if you want to generate attributes containing curlies, you need to escape them by doubling up:

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

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

However, including large blocks of Javascript inside onXXX attributes is probably best avoided anyway: instead, just include a function call to code defined within <script>.

这篇关于在XSL中包含带有Curly Brackets {}的内联JavaScript的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆