apply-string-subst-map

apply-string-subst-map — Apply a string-substitution map

Description

This function applies a “string substitution” map. Use it when you want to do multiple string substitutions on the same target content. It reads in two things: content, the content on which to perform the substitution, and map.contents, a node set of elements (the names of the elements don't matter), with each element having the following attributes:

  • oldstring, a string to be replaced
  • newstring, a string with which to replace oldstring

The function uses map.contents to do substitution on content, and then returns the modified contents.

Note

This function is a very slightly modified version of Jeni Tennison’s replace_strings function in the multiple string replacements section of Dave Pawson’s XSLT FAQ.

The apply-string-subst-map function is essentially the same function as the apply-character-map function; the only difference is that in the map that apply-string-subst-map expects, oldstring and newstring attributes are used instead of character and string attributes.

    <xsl:template name="apply-string-subst-map">
      <xsl:param name="content"></xsl:param>
      <xsl:param name="map.contents"></xsl:param>
      <xsl:variable name="replaced_text">
        <xsl:call-template name="string.subst">
          <xsl:with-param name="string" select="$content"></xsl:with-param>
          <xsl:with-param name="target" select="$map.contents[1]/@oldstring"></xsl:with-param>
          <xsl:with-param name="replacement" select="$map.contents[1]/@newstring"></xsl:with-param>
        </xsl:call-template>
      </xsl:variable>
      <xsl:choose>
        <xsl:when test="$map.contents[2]">
          <xsl:call-template name="apply-string-subst-map">
            <xsl:with-param name="content" select="$replaced_text"></xsl:with-param>
            <xsl:with-param name="map.contents" select="$map.contents[position() > 1]"></xsl:with-param>
          </xsl:call-template>
        </xsl:when>
        <xsl:otherwise>
          <xsl:value-of select="$replaced_text"></xsl:value-of>
        </xsl:otherwise>
      </xsl:choose>
    </xsl:template>