问题描述
我正在尝试通过 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?我知道您可以使用 来转义 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 表达式.所以如果你想生成包含卷曲的属性,你需要通过加倍来转义它们:
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:相反,只需包含对 中定义的代码的函数调用.
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 中包含带有花括号 {} 的内联 JavaScript的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!