Export (0) Print
Expand All
5 out of 6 rated this helpful - Rate this topic

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.

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.

CautionCaution:
Stringconversions 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" 

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.




Build Date:

2012-08-02
Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2014 Microsoft. All rights reserved.