XSLT:XML:替换功能/功能

时间:2011-05-06 18:12:53

标签: xml xslt

我想提供以下html

words<block>words</block>

从xml doc中的此节点

<text>words bli!wordsbli!</text>
bli!表示将在xml doc的节点中随机出现的标记。是否有可能取代bli!使用xslt函数?

1 个答案:

答案 0 :(得分:1)

XSLT 1.0中没有内置任何基本字符串替换功能。

This post是实现此功能的好方法。

 <xsl:template name="string-replace-all">
    <xsl:param name="text" />
    <xsl:param name="replace" />
    <xsl:param name="by" />
    <xsl:choose>
      <xsl:when test="contains($text, $replace)">
        <xsl:value-of select="substring-before($text,$replace)" />
        <xsl:value-of select="$by" />
        <xsl:call-template name="string-replace-all">
          <xsl:with-param name="text"
          select="substring-after($text,$replace)" />
          <xsl:with-param name="replace" select="$replace" />
          <xsl:with-param name="by" select="$by" />
        </xsl:call-template>
      </xsl:when>
      <xsl:otherwise>
        <xsl:value-of select="$text" />
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template>

以下是它的调用方式:

  <xsl:variable name="myVar">
    <xsl:call-template name="string-replace-all">
      <xsl:with-param name="text" select="'This is a sample text : {ReplaceMe} and {ReplaceMe}'" />
      <xsl:with-param name="replace" select="'{ReplaceMe}'" />
      <xsl:with-param name="by" select="'String.Replace() in XSLT'" />
    </xsl:call-template>
  </xsl:variable>