json - XSLT 1.0 转义字符串中的双引号和反斜杠

标签 json xslt xslt-1.0

我有一个如下所示的字符串并尝试转换为 json 格式:

测试“out”和\new

预期输出是
测试\"out\"和\new

我尝试通过调用转义引号模板 - 转义引号工作正常:

<xsl:template name="escapeQuote">
    <xsl:param name="pText" select="concat(normalize-space(.), '')" />
    <xsl:if test="string-length($pText) >0">
        <xsl:value-of select="substring-before(concat($pText, '&quot;'), '&quot;')" />

        <xsl:if test="contains($pText, '&quot;')">
            <xsl:text>\"</xsl:text>    
            <xsl:call-template name="escapeQuote">
                <xsl:with-param name="pText" select="substring-after($pText, '&quot;')" />
            </xsl:call-template>
        </xsl:if>
    </xsl:if>
</xsl:template>

转义反斜杠模板 - 仅适用于反斜杠:
<xsl:template name="jsonescape">
 <xsl:param name="str" select="."/>
  <xsl:choose>
    <xsl:when test="contains($str, '\')">
      <xsl:value-of select="concat(substring-before($str, '\'), '\\' )"/>
      <xsl:call-template name="jsonescape">
        <xsl:with-param name="str" select="substring-after($str, '\')"/>
      </xsl:call-template>
    </xsl:when>
    <xsl:otherwise>
        <xsl:value-of select="$str"/>
    </xsl:otherwise>
  </xsl:choose>
</xsl:template>

我的问题是如何调用两个模板或合并,请帮助我

最佳答案

这是一个如何组合两个模板调用的示例,以便 jsonescape 的输出用作 escapequote 的输入参数

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0">
  <xsl:output method="text" />

  <xsl:template match="input">
    <xsl:call-template name="escapeQuote">
      <xsl:with-param name="pText">
        <xsl:call-template name="jsonescape">
          <xsl:with-param name="str" select="." />
        </xsl:call-template>          
      </xsl:with-param>
    </xsl:call-template>
  </xsl:template>

  <xsl:template name="escapeQuote">
      <xsl:param name="pText" select="concat(normalize-space(.), '')" />
      <xsl:if test="string-length($pText) >0">
          <xsl:value-of select="substring-before(concat($pText, '&quot;'), '&quot;')" />

          <xsl:if test="contains($pText, '&quot;')">
              <xsl:text>\"</xsl:text>    
              <xsl:call-template name="escapeQuote">
                  <xsl:with-param name="pText" select="substring-after($pText, '&quot;')" />
              </xsl:call-template>
          </xsl:if>
      </xsl:if>
  </xsl:template>

  <xsl:template name="jsonescape">
   <xsl:param name="str" select="."/>
    <xsl:choose>
      <xsl:when test="contains($str, '\')">
        <xsl:value-of select="concat(substring-before($str, '\'), '\\' )"/>
        <xsl:call-template name="jsonescape">
          <xsl:with-param name="str" select="substring-after($str, '\')"/>
        </xsl:call-template>
      </xsl:when>
      <xsl:otherwise>
          <xsl:value-of select="$str"/>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template>
</xsl:stylesheet>

所以,给定一个输入:
<input>Test "out" and \new</input>

以下是输出
Test \"out\" and \\new

注意顺序很重要,因为如果你颠倒了模板调用的顺序,"将被转换为 \"escapequote模板,然后将转换为 \\"jsonescape模板。

或者,因为两个模板都做类似的事情,放置一个 \在特定字符之前,您可以将两个模板合二为一。

也试试这个 XSLT
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0">
  <xsl:output method="text" />

  <xsl:template match="input">
    <xsl:call-template name="jsonescape">
      <xsl:with-param name="str" select="." />
    </xsl:call-template>          
  </xsl:template>

  <xsl:template name="jsonescape">
   <xsl:param name="str" select="."/>
   <xsl:param name="escapeChars" select="'\&quot;'" />
   <xsl:variable name="first" select="substring(translate($str, translate($str, $escapeChars, ''), ''), 1, 1)" />
   <xsl:choose>
      <xsl:when test="$first">
        <xsl:value-of select="concat(substring-before($str, $first), '\', $first)"/>
        <xsl:call-template name="jsonescape">
          <xsl:with-param name="str" select="substring-after($str, $first)"/>
        </xsl:call-template>
      </xsl:when>
      <xsl:otherwise>
          <xsl:value-of select="$str"/>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template>
</xsl:stylesheet>

关于json - XSLT 1.0 转义字符串中的双引号和反斜杠,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37452696/

相关文章:

xml - 为什么我的图像没有显示在 XSL-FO 中?

ios - 为什么从 NSMutableArray 设置 labels.text 不起作用?

json - 如何将数据框转换为自定义 json 格式

xslt - 输入 XML 的动态命名空间

javascript - XSLT - 自动打开 HREF

XSLT:reocurring xpath的缩写

java - 无法使用 Java/Hibernate 反序列化 NativeQuery 中的 DateTime

mysql - 关联关联 : include entity in JSON response

regex - 正则表达式帮助

java - 将 XML 从一种结构转换为另一种结构