contains() Function |
Determines if the first argument string contains the second.
Inputs
Two strings. If the first string contains the second string, the function returns the boolean value true.
Output
The boolean value true if the first argument contains the second; false otherwise.
Defined in
XPath section 4.2, String Functions.
Example
This stylesheet uses the replace-substring named template. It passes three arguments to the replace-substring template: the original string, the substring to be searched for in the original string, and the substring to replace the target substring in the original string. The replace-substring template uses the contains(), substring-after(), and substring-before() functions extensively. Here is our sample stylesheet. It replaces all occurrences of World with the string "Mundo":
    <?xml version="1.0"?>
    <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">

      <xsl:output method="text"/>

      <xsl:template match="/">
        <xsl:variable name="test">
          <xsl:call-template name="replace-substring">
            <xsl:with-param name="original">Hello World!</xsl:with-param>
            <xsl:with-param name="substring">World</xsl:with-param>
            <xsl:with-param name="replacement">Mundo</xsl:with-param>
          </xsl:call-template>
        </xsl:variable>
        <xsl:value-of select="$test"/>
      </xsl:template>

      <xsl:template name="replace-substring">
        <xsl:param name="original"/>
        <xsl:param name="substring"/>
        <xsl:param name="replacement" select="''"/>
        <xsl:variable name="first">
          <xsl:choose>
            <xsl:when test="contains($original, $substring)">
              <xsl:value-of select="substring-before($original, $substring)"/>
            </xsl:when>
            <xsl:otherwise>
              <xsl:value-of select="$original"/>
            </xsl:otherwise>
          </xsl:choose>
        </xsl:variable>
        <xsl:variable name="middle">
          <xsl:choose>
            <xsl:when test="contains($original, $substring)">
              <xsl:value-of select="$replacement"/>
            </xsl:when>
            <xsl:otherwise>
              <xsl:text></xsl:text>
            </xsl:otherwise>
          </xsl:choose>
        </xsl:variable>
        <xsl:variable name="last">
          <xsl:choose>
            <xsl:when test="contains($original, $substring)">
              <xsl:choose>
                <xsl:when test="contains(substring-after($original,
                                            $substring), $substring)">
                  <xsl:call-template name="replace-substring">
                    <xsl:with-param name="original">
                      <xsl:value-of
                        select="substring-after($original, $substring)"/>
                    </xsl:with-param>
                    <xsl:with-param name="substring">
                      <xsl:value-of select="$substring"/>
                    </xsl:with-param>
                    <xsl:with-param name="replacement">
                      <xsl:value-of select="$replacement"/>
                    </xsl:with-param>
                  </xsl:call-template>
                </xsl:when>
                <xsl:otherwise>
                  <xsl:value-of
                    select="substring-after($original, $substring)"/>
                </xsl:otherwise>
              </xsl:choose>
            </xsl:when>
            <xsl:otherwise>
              <xsl:text></xsl:text>
            </xsl:otherwise>
          </xsl:choose>
        </xsl:variable>
        <xsl:value-of select="concat($first, $middle, $last)"/>
      </xsl:template>

    </xsl:stylesheet>
The stylesheet produces these results, regardless of the XML document used as input:
    Hello Mundo!