contains 函数 (XPath)
检查第一个参数字符串是否包含第二个参数字符串。
boolean contains(str1, str2)
参数
str1
可能包含第二个参数的字符串。str2
可能包含在第一个参数中的字符串。
返回值
如果第一个参数字符串包含第二个参数字符串,则返回 true。 否则,返回 false。
备注
如果参数不是字符串类型,将先使用 string() 函数转换为字符串,然后计算该转换的结果。
警告
作为参数传递给此函数的节点集的字符串转换可能会产生意外的结果。有关详细信息,请参阅string 函数 (XPath)。
此函数区分大小写。
示例
以下示例阐释如何使用 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>
输出
应用于 XML 文件 (contains.xml) 时,上面的 XSLT 样式表将产生以下输出:
The Weather Pattern by Weather Man costs 100.00.
Weaving Patterns by Weaver costs 150.00.
Speech Pattern by Speaker costs 15.00.