单引号转义 XSLT 1.0 中的动态字符串值 [英] Single quote escaping a dynamic value-of string in XSLT 1.0

查看:19
本文介绍了单引号转义 XSLT 1.0 中的动态字符串值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在从 XSLT 定义一个 JavaScript 变量,但由于未转义的字符串而出现错误.我知道我需要将其替换为 ',但我不确定如何在 XSLT 1.0 中做到这一点.

I am defining a JavaScript variable from XSLT and am getting an error due to an unescaped string. I understand that I need to replace this to ', but I'm unsure how to do that in XSLT 1.0.

XSLT 示例:

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

使用未转义的单引号渲染 javascript:

Rendered javascript with unescaped single quote:

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

推荐答案

正如 Ian Roberts 在评论中指出的那样,您需要考虑反斜杠,而不仅仅是撇号.Dimitre 的回答可以修改如下以说明这一点:

As Ian Roberts pointed out in his comment, you need to account for backslashes and not just apostrophes. Dimitre's answer can be modified as follows to account for this:

<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-templatesescape 模式.对于此输入 XML:

Any time you need to escape something, you can just use apply-templates on it with mode escape. For this input 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 \\\\\\\\'

这篇关于单引号转义 XSLT 1.0 中的动态字符串值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆