2013-05-08 99 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>'