contains Function

Checks whether the first argument string contains the second argument string.

boolean contains(str1, str2)


  • str1
    A string that might contain the second argument.
  • str2
    A string that might be contained in the first argument.

Return Values

Returns true if the first argument string contains the second argument string. Returns false otherwise.


If an argument is not of type string, it is first converted to a string using the string() function and then the result of that conversion is evaluated.


String conversions for node sets that are passed as arguments to this function may yield unexpected results. For more information, see string Function.

This function is case-sensitive.


The following example illustrates how to use the contains() function to query a collection of books whose titles contain the word "Pattern".

XML FILE (contains.xml)

<?xml version="1.0"?>
<?xml-stylesheet type="text/xsl" 

     <title>The Weather Pattern</title>
     <author>Weather Man</author>
     <title>Weaving Patterns</title>
     <title>Speech Pattern</title>
     <title>Writing Style</title>

XSLT FILE (contains.xsl)

<?xml version='1.0'?>
<xsl:stylesheet version="1.0"           


  <xsl:output method="html"   

  <xsl:template match="/">
       <xsl:apply-templates select="//book"/>

  <xsl:template match="book">
     <xsl:if test="contains(title, 'Pattern')">
         <B><xsl:value-of select="title"/></B> by 
         <I><xsl:value-of select="author"/></I> costs
         <xsl:value-of select="price"/>.



When applied to the XML file (contains.xml), the XSLT style sheet above produces the following output:

The Weather Pattern by Weather Man costs 100.00.

Weaving Patterns by Weaver costs 150.00.

Speech Pattern by Speaker costs 15.00.

See Also


string Function
XML Data Types Reference