4

我正在尝试使用 XSL translate() 函数来创建类似搜索和替换函数的内容,如下所示:

<xsl:template name="create-id">
    <xsl:param name="id" />
    <xsl:call-template name="search-and-replace">
        <xsl:with-param name="str" select="$id" />
        <xsl:with-param name="search">0123456789</xsl:with-param>
        <xsl:with-param name="replace">abcdefghij</xsl:with-param>
    </xsl:call-template>
</xsl:template>

<xsl:template name="search-and-replace">
    <xsl:param name="str" />
    <xsl:param name="search" />
    <xsl:param name="replace" />
    <xsl:variable name="newstr" select="translate($str, $search,
    $replace)" />
    <xsl:choose>
        <xsl:when test="contains($newstr, $search)">
            <xsl:call-template name="search-and-replace">
                <xsl:with-param name="str" select="$newstr" />
                <xsl:with-param name="search" select="$search" />
                <xsl:with-param name="replace" select="$replace" />
            </xsl:call-template>
        </xsl:when>
        <xsl:otherwise>
            <xsl:value-of select="$newstr" />
        </xsl:otherwise>
    </xsl:choose>
</xsl:template>

但是,我的逻辑在这里有些错误,因为它似乎剥离了返回字符串中的最后一个字符。我的猜测是 translate() 只是替换字符串中每个字符的第一个实例,并不是真正的递归。

任何想法或意见将不胜感激。

4

4 回答 4

9

translate()函数只能用另一个单个字符(或空字符(删除))替换单个字符。因此它不能解决字符串替换的问题。

这是针对多替换问题的完整 XSLT 1.0 解决方案

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

    <my:params xml:space="preserve">
        <pattern>
            <old>&#xA;</old>
            <new><br/></new>
        </pattern>
        <pattern>
            <old>quick</old>
            <new>slow</new>
        </pattern>
        <pattern>
            <old>fox</old>
            <new>elephant</new>
        </pattern>
        <pattern>
            <old>brown</old>
            <new>white</new>
        </pattern>
    </my:params>

    <xsl:variable name="vPats"
         select="document('')/*/my:params/*"/>

    <xsl:template match="text()" name="multiReplace">
        <xsl:param name="pText" select="."/>
        <xsl:param name="pPatterns" select="$vPats"/>

        <xsl:if test="string-length($pText) >0">
            <xsl:variable name="vPat" select=
            "$vPats[starts-with($pText, old)][1]"/>

            <xsl:choose>
                <xsl:when test="not($vPat)">
                    <xsl:copy-of select="substring($pText,1,1)"/>
                </xsl:when>
                <xsl:otherwise>
                    <xsl:copy-of select="$vPat/new/node()"/>
                </xsl:otherwise>
            </xsl:choose>

            <xsl:call-template name="multiReplace">
                <xsl:with-param name="pText" select=
                "substring($pText, 1 + not($vPat) + string-length($vPat/old/node()))"/>
            </xsl:call-template>
        </xsl:if>
    </xsl:template>
</xsl:stylesheet>

当此转换应用于以下 XML 文档时:

<t>The quick
brown fox</t>

产生了想要的正确结果

The slow<br />white elephant

说明

  1. 使用递归调用自身的命名模板。

  2. 所有多个替换模式 --> 替换对都在单个外部参数中提供,为了方便,这里将其指定为内联的全局级元素<my:params>

  3. 递归获取源字符串中的每个字符(从左到右)并在字符串中的该位置找到以该字符开头的第一个模式。

  4. 替换不仅可以是字符串,还可以是任何节点。在这种特定情况下,我们将每个 NL 字符替换为一个<br/>元素。

于 2011-03-12T16:24:27.100 回答
3

函数的定义translate($arg, $mapString, $transString)是:

返回$argmodified 的值,以便 的值中$arg 出现在 的值中某个位置 N 处的每个字符$mapString都已被出现在 的值中的位置 N 处的字符替换 $transString

也就是说,它不会用另一个字符串替换子字符串,而是将字符映射到其他字符。对于子字符串替换,使用类似

<xsl:template name="search-and-replace">
  <xsl:param name="str"/>
  <xsl:param name="search"/>
  <xsl:param name="replace"/>
  <xsl:choose>
    <xsl:when test="contains($str, $search)">
      <xsl:value-of select="substring-before($str, $search)"/>
      <xsl:value-of select="$replace"/>
      <xsl:call-template name="search-and-replace">
        <xsl:with-param name="str" select="substring-after($str, $search)"/>
        <xsl:with-param name="search" select="$search"/>
        <xsl:with-param name="replace" select="$replace"/>
      </xsl:call-template>
    </xsl:when>
    <xsl:otherwise>
      <xsl:value-of select="$str"/>
    </xsl:otherwise>
  </xsl:choose>
</xsl:template>
于 2011-03-12T09:22:04.233 回答
0

似乎您永远不会是真的,因为您已经将 $search 中的每个字符替换为 $replace in

<xsl:variable name="newstr" select="translate($str, $search,
    $replace)" />

预先。

于 2011-03-12T04:16:20.963 回答
0

str:从 exslt 替换。它完全符合您的要求,并且已经由一些 xslt 专家编写和测试。xslt 源代码也可以从那里获得。

于 2011-03-12T16:29:50.327 回答