在XSL中包含带花括号{}的内联JavaScript
我试图通过XSL文件将XML转换为HTML。不幸的是,它不允许我使用JavaScript花括号{}。下面是一个简单的示例,但我的实际代码要大得多在XSL中包含带花括号{}的内联JavaScript,javascript,xml,xslt,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:
<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;}
预期标记“}”,发现“true”。。。firm('you sure?'){return-->true以转义Javascript块。但是如何转义内联Javascript?我的实际代码太大,无法将所有内联Javascript重新写入脚本块。试试这个,它不需要转义:
<body>
<xsl:attribute name="onload">
javascript: if(confirm('Are you sure?')) { return true;} else { return false;}
</xsl:attribute>
</body>
javascript:if(confirm('you sure?'){return true;}else{return false;}
文本结果元素的属性中的花括号用于“属性值模板”,包含XPath表达式。因此,如果要生成包含花括号的属性,需要通过将它们加倍来转义:
<body onload="javascript: if(confirm('Are you sure?')) {{ return true;}} else {{ return false;}}">
不过,最好还是避免在onXXX属性中包含大块Javascript:相反,只需包含对
中定义的代码的函数调用,谢谢Michael。不幸的是,我无法控制HTML文件,因此您的解决方案工作得最好,因为它是一个简单的搜索和替换。