5

我正在从 XSLT 定义一个 JavaScript 变量,并且由于未转义的字符串而出现错误。我知道我需要将其替换为',但我不确定如何在 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 ------^
4

3 回答 3

7

正如 Ian Roberts 在他的评论中指出的那样,您需要考虑反斜杠而不仅仅是撇号。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-templatesmode 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 \\\\\\\\'
于 2013-01-12T15:42:35.280 回答
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.'
于 2013-01-12T04:52:11.207 回答
0

这只是我为完成我的 xml 所做的 JLRische-Novachev 解决方案的一个实现。也许它可能有用:

<?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
于 2014-09-29T13:54:53.067 回答