[英]Include Inline JavaScript with Curly Brackets {} in XSL
我正在尝试通过 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>
Visual Studio 给了我以下错误:
Expected token '}', found 'true'. ...firm('Are you sure?')) { return -->true<-- ;} else { return false;}
有没有办法在 XSL 中包含内联 JavaScript? 我知道您可以使用<![CDATA[ ]]>
来转义 Javascript 块。 但是如何转义内联 JavaScript? 我的实际代码太大了,无法将所有内联 JavaScript 重写为脚本块。
文字结果元素属性中的花括号用于“属性值模板”,包含 XPath 表达式。 所以如果你想生成包含卷曲的属性,你需要通过加倍来转义它们:
<body onload="javascript: if(confirm('Are you sure?')) {{ return true;}} else {{ return false;}}">
但是,最好避免在 onXXX 属性中包含大块 Javascript:相反,只需包含对<script>
定义的代码的函数调用。
试试这个,它应该不需要转义:
<body>
<xsl:attribute name="onload">
javascript: if(confirm('Are you sure?')) { return true;} else { return false;}
</xsl:attribute>
</body>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.