2013-05-08 94 views
1

我使用的JavaScript是这样的:JavaScript变量= XSL价值的

<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="." />" OR "<xsl:value-of select='.' />"

现在我知道撇号'与JavaScript代码发生冲突,但哪种解决方案可以在所有浏览器中使用?

回答

2

您可以使用'<xsl:value-of select="." />'但你需要在前面加上这样\'

可以使用"<xsl:value-of select="." />" or "<xsl:value-of select='.' />"斜线逃脱在<alldata>所有单引号撇号,但如果有机会的<alldata>将包含双引号,那么你将需要逃生者为好,这样\"

如果你想使用的第一个,那么这将逃脱单引号:

<xsl:template name="escapeSingleQuotes"> 
    <xsl:param name="txt"/> 

    <xsl:variable name="backSlashSingleQuote">&#92;&#39;</xsl:variable> 
    <xsl:variable name="singleQuote">&#39;</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>'