2013-01-11 42 views
5

我正在從XSLT定義JavaScript變量,並且由於未轉義的字符串而出現錯誤。我知道我需要將其替換爲',但我不確定如何在XSLT 1.0中執行此操作。單引號在XSLT 1.0中轉義動態值字符串

XSLT例如:

var currentComment = '<xsl:value-of select="root/Reviews/Review/Comment" />'; 

渲染JavaScript和未轉義單引號:

var currentComment = 'Let's test a string.', 
// Causing an error ------^ 
+1

你需要用'更換'''\ '',而不是'''(還有e非常反斜槓和雙反斜槓) - 您正在從JavaScript解析器中轉義它,而不是HTML解析器。看看[這個答案](http://stackoverflow.com/a/1069157/592139)可能的XSLT 1.0解決方案。 –

回答

7

正如伊恩·羅伯茨在他的評論中指出的那樣,你需要考慮反斜槓,而不只是撇號。 Dimitre的回答可以作如下修改考慮到這一點:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
    <xsl:output omit-xml-declaration="yes" indent="yes"/> 

    <xsl:template match="/"> 
    <xsl:text>var currentComment = '</xsl:text> 
    <xsl:apply-templates select="root/value" mode="escape" /> 
    <xsl:text>'&#xA;</xsl:text> 

    <!-- Example with placing the escaped value in a variable first --> 
    <xsl:variable name="escapedOther"> 
     <xsl:apply-templates select="root/otherValue" mode="escape" /> 
    </xsl:variable> 
    <xsl:value-of select='concat("var otherComment = &apos;", $escapedOther, "&apos;")' /> 
    </xsl:template> 


    <xsl:template match="@* | node()" mode="escape"> 
    <!-- Escape the apostrophes second --> 
    <xsl:call-template name="replace"> 
     <xsl:with-param name="pTarget" select='"&apos;"' /> 
     <xsl:with-param name="pReplacement" select='"\&apos;"'/> 
     <xsl:with-param name="pText"> 
     <!-- Escape the backslashes first, and then pass that result directly into the next template --> 
     <xsl:call-template name="replace"> 
      <xsl:with-param name="pTarget" select="'\'" /> 
      <xsl:with-param name="pReplacement" select="'\\'" /> 
      <xsl:with-param name="pText" select="." /> 
     </xsl:call-template> 
     </xsl:with-param> 
    </xsl:call-template> 
    </xsl:template> 

    <xsl:template name="replace"> 
    <xsl:param name="pText"/> 
    <xsl:param name="pTarget" select='"&apos;"'/> 
    <xsl:param name="pReplacement" select="'\&quot;'"/> 

    <xsl:if test="$pText"> 
     <xsl:value-of select='substring-before(concat($pText,$pTarget),$pTarget)'/> 
     <xsl:if test='contains($pText, $pTarget)'> 
     <xsl:value-of select='$pReplacement'/> 
     </xsl:if> 

     <xsl:call-template name="replace"> 
     <xsl:with-param name="pText" select='substring-after($pText, $pTarget)'/> 
     <xsl:with-param name="pTarget" select="$pTarget"/> 
     <xsl:with-param name="pReplacement" select="$pReplacement"/> 
     </xsl:call-template> 
    </xsl:if> 
    </xsl:template> 
</xsl:stylesheet> 

你需要逃避的東西任何時候,你可以直接使用就可以了apply-templates與模式escape。對於這種輸入XML:

<?xml version="1.0" encoding="utf-8"?> 
<root> 
    <value>Let's test a string that refers to something like the C:\ drive's \Program Files Directory.</value> 
    <otherValue>This value has a bunch of apostrophes '''' and backslashes \\\\ in it</otherValue> 
</root> 

這將產生:

var currentComment = 'Let\'s test a string that refers to something like the C:\\ drive\'s \\Program Files Directory.' 
var otherComment = 'This value has a bunch of apostrophes \'\'\'\' and backslashes \\\\\\\\' 
+1

+1「for reusing」my code :) –

3

該轉化

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
<xsl:output omit-xml-declaration="yes" indent="yes"/> 

<xsl:variable name="vSomething">Let's test a string.</xsl:variable> 

<xsl:template match="/"> 
    <xsl:text>var currentComment = '</xsl:text> 
    <xsl:call-template name="replace"> 
    <xsl:with-param name="pText" select="$vSomething"/> 
    </xsl:call-template>' 
</xsl:template> 

<xsl:template name="replace"> 
    <xsl:param name="pText"/> 
    <xsl:param name="pTarget" select='"&apos;"'/> 
    <xsl:param name="pReplacement" select='"\&apos;"'/> 

    <xsl:if test="$pText"> 
    <xsl:value-of select='substring-before(concat($pText,$pTarget),$pTarget)'/> 
    <xsl:if test='contains($pText, $pTarget)'> 
    <xsl:value-of select="$pReplacement"/> 
    </xsl:if> 

    <xsl:call-template name="replace"> 
    <xsl:with-param name="pText" select='substring-after($pText, $pTarget)'/> 
    <xsl:with-param name="pTarget" select="$pTarget"/> 
    <xsl:with-param name="pReplacement" select="$pReplacement"/> 
    </xsl:call-template> 
    </xsl:if> 
</xsl:template> 
</xsl:stylesheet> 

當在任何XML文檔應用(未使用),產生想,正確的結果:

var currentComment = 'Let\'s test a string.' 
+0

爲了使'替換'模板成爲動態的,我們很難定義參數'$ pTarget'和'$ pReplacement',但是實際上並沒有在重要的地方使用它們。 – JLRishe

+0

@JLRishe,是的,這是*設計* :)請注意,我給這些參數適當的*默認值*。所以,結果是我可以在這個特殊情況下在模板的最外層調用中省略它們。 –

+0

我不是指從模板調用中忽略它們 - 我指的是在這些行中根本不使用它們:'','',''。如果有人真的爲這些參數傳入值,它仍然只是執行'''''''''''替換。 – JLRishe

0

這僅僅是JLRische-Novachev解決方案我沒有完成我的XML的實現。 也許它可以是有用的:

<?xml version="1.0" encoding="utf-8"?> 
<root> 
    <value msg="1- Let's test a string that refers to something like the C:\ drive's \Program  Files Directory." num="1"/> 
    <value msg="2- Let's test a string that refers to something like the C:\ drive's \Program Files Directory." num="2"/> 
</root> 

的xsl:

... 
<xsl:template match="@* | node()" mode="escape"> 

    <!-- Escape the apostrophes second --> 
    <xsl:call-template name="replace"> 
     <xsl:with-param name="pTarget" select='"&apos;"' /> 
     <xsl:with-param name="pReplacement" select='"\&apos;"'/> 
     <xsl:with-param name="pText"> 
     <!-- Escape the backslashes first, and then pass that result directly into the next  template --> 
     <xsl:call-template name="replace"> 
      <xsl:with-param name="pTarget" select="'\'" /> 
      <xsl:with-param name="pReplacement" select="'\\'" /> 
      <xsl:with-param name="pText" select="./@msg" /> 
     </xsl:call-template> 
     </xsl:with-param> 
    </xsl:call-template> 

<xsl:text>, Number: </xsl:text><xsl:value-of select="./@num"/><xsl:text>&#xA;</xsl:text> 

... 

輸出:

1- Let\'s test a string that refers to something like the C:\\ drive\'s \\Program Files Directory., Number: 1 
2- Let\'s test a string that refers to something like the C:\\ drive\'s \\Program Files Directory., Number: 2