substring-before() Function |
Returns the substring of the first argument before the first occurrence of the second argument in the first argument. If the second argument does not occur in the first argument, the substring-before() function returns an empty string.
Inputs
Two strings. The first string is the string to be searched, and the second string is the string to be searched for in the first string.
Output
The portion of the first argument that occurs before the first occurrence of the second argument. If the second argument does not appear in the first argument, the function returns an empty string.
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!