contains 函式
檢查第一個引數字串是否包含第二個引數字串。
boolean contains(str1, str2)
參數
str1
可能包含第二個引數的字串。str2
可能包含在第一個引數中的字串。
傳回值
若第一個引數字串包含第二個引數字串,則會傳回 True。否則會傳回 False。
備註
如果引數的型別不是 string,則會先使用 string() 函式將它轉換成字串,然後再評估該轉換的結果。
注意
當作引數傳遞至此函式的節點集字串轉換,有可能會產生非預期的結果。如需詳細資訊,請參閱 string 函式。
此函式有區分大小寫。
範例
下列範例將說明如何使用 contains() 函式來查詢標題中含有 "Pattern" 這個字的書籍集合。
XML 檔 (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 檔 (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>
輸出
當上述 XSLT 樣式表套用至 XML 檔 (contains.xml) 時,就會產生下列輸出:
The Weather Pattern by Weather Man costs 100.00.
Weaving Patterns by Weaver costs 150.00.
Speech Pattern by Speaker costs 15.00.