Share via


contains Function 

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

boolean contains(str1, str2)

Parameters

  • 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.

Remarks

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.

Warning

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.

Example

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" 

href="contains.xsl"?>      
<bookstore>
  <book>
     <title>The Weather Pattern</title>
     <author>Weather Man</author>
     <price>100.00</price>
  </book>
  <book>
     <title>Weaving Patterns</title>
     <author>Weaver</author>
     <price>150.00</price>
  </book>
  <book>
     <title>Speech Pattern</title>
     <author>Speaker</author>
     <price>15.00</price>
  </book>
  <book>
     <title>Writing Style</title>
     <author>Writer</author>
     <price>1500.00</price>
  </book>
</bookstore>

XSLT FILE (contains.xsl)

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

xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

  <xsl:output method="html"   
     omit-xml-declaration="yes"/>

  <xsl:template match="/">
    <html>
       <head><title>example</title></head>
    <body>
       <xsl:apply-templates select="//book"/>
    </body>
    </html>
  </xsl:template>

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

</xsl:stylesheet>

Output

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

Reference

string Function
XML Data Types Reference