我使用的JavaScript是这样的:
<script>
<xsl:for-each select = '/request/alldata'>
var l_allDataValue = '<xsl:value-of select="." />';
var l_dataArray = l_allDataValue.split('!~');
callFunction(l_dataArray);
</xsl:for-each>
</script>但是如果/request/alldata中有撇号',它将中断JavaScript,因为下面的表达式包含在撇号中:
'<xsl:value-of select="." />'但如果我将其替换为以下任一项,则可以正常工作...
"<xsl:value-of select="." />"或"<xsl:value-of select='.' />"
现在我知道撇号'与JavaScript代码冲突了,但是哪种解决方案可以在所有浏览器上运行?
发布于 2013-05-08 15:51:23
您可以使用'<xsl:value-of select="." />',但您需要通过在<alldata>中添加一个斜杠来转义所有单引号撇号,例如\'
您可以使用"<xsl:value-of select="." />" or "<xsl:value-of select='.' />",但是如果<alldata>可能包含双引号,那么您也需要转义这些双引号,比如下面的\"
如果您想要使用第一个,那么这将转义单引号:
<xsl:template name="escapeSingleQuotes">
<xsl:param name="txt"/>
<xsl:variable name="backSlashSingleQuote">\'</xsl:variable>
<xsl:variable name="singleQuote">'</xsl:variable>
<xsl:choose>
<xsl:when test="string-length($txt) = 0">
<!-- empty string - do nothing -->
</xsl:when>
<xsl:when test="contains($txt, $singleQuote)">
<xsl:value-of disable-output-escaping="yes"
select="concat(substring-before($txt, $singleQuote), $backSlashSingleQuote)"/>
<xsl:call-template name="escapeSingleQuotes">
<xsl:with-param name="txt" select="substring-after($txt, $singleQuote)"/>
</xsl:call-template>
</xsl:when>
<xsl:otherwise>
<xsl:value-of disable-output-escaping="yes" select="$txt"/>
</xsl:otherwise>
</xsl:choose>
</xsl:template>你可以像这样在你的代码中使用:
var l_allDataValue = '<xsl:call-template name="escapeSingleQuotes">
<xsl:with-param name="txt" select="."/>
</xsl:call-template>'https://stackoverflow.com/questions/16434669
复制相似问题