XSL多重搜索和替换功能

时间:2011-03-12 01:03:15

标签: search xslt replace translate

我正在尝试使用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 个答案:

答案 0 :(得分:8)

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. 所有多重替换模式 - &gt;替换对在单个外部参数中提供,为方便起见,此处将内联指定为全局级元素<my:params>

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

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

答案 1 :(得分:3)

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

  

返回修改后的$arg的值   $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>

答案 2 :(得分:0)

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

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

预先。

答案 3 :(得分:0)

str:replace from exslt。它完全符合您的要求,并且已经由一些xslt专家编写和测试。 xslt源也可用there

相关问题