Previous | Next | Indexes
Zvon > Tutorials > XSLT 2.0 Tutorial > Functions operating on strings (3/15) >

Comparing starts and ends of strings

The functions starts-with() and ends-with() test whether the first argument starts or ends with the second one. The comparisons are case-sensitive.

XSLT

      <xsl:stylesheet
                  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                  version="2.0">
            <xsl:output  method="xml"
                        indent="yes"
                        omit-xml-declaration="yes"/>

            <xsl:template  match="/aaa">
                  <xxx>
                        <i>
                              <xsl:value-of  select="starts-with(bbb,'Hell')"/>
                        </i>
                        <ii>
                              <xsl:value-of  select="starts-with(bbb,'hell')"/>
                        </ii>
                  </xxx>
                  <yyy>
                        <i>
                              <xsl:value-of  select="ends-with(bbb,'!')"/>
                        </i>
                        <ii>
                              <xsl:value-of  select="ends-with(ddd,'.xml')"/>
                        </ii>
                  </yyy>
                  <zzz>
                        <i>
                              <xsl:value-of  select="starts-with(bbb,ccc)"/>
                        </i>
                        <ii>
                              <xsl:value-of  select="starts-with(ddd,lower-case(ccc))"/>
                        </ii>
                  </zzz>
            </xsl:template>

      </xsl:stylesheet>
XML

      <aaa>
            <bbb>Hello, world!</bbb>
            <ccc>Hello</ccc>
            <ddd>hello.xml</ddd>
      </aaa>
Output

      <xxx>
            <i>true</i>
            <ii>false</ii>
      </xxx>
      <yyy>
            <i>true</i>
            <ii>true</ii>
      </yyy>
      <zzz>
            <i>true</i>
            <ii>true</ii>
      </zzz>


Previous chapter: Arithmetics
Next chapter: Transforming strings with regular expressions
Previous page: Testing for a substring in a string with contains function
Next page: Comparisons of two strings with function compare