starts-with 函数 (XPath)
如果第一个参数字符串以第二个参数字符串开头,则返回 true;否则,返回 false。
boolean starts-with(string, string)
备注
如果参数不是字符串类型,将先使用 string() 函数转换为字符串,然后计算该转换的结果。
警告
作为参数传递给此函数的节点集的字符串转换可能会产生意外的结果。有关详细信息,请参阅string 函数 (XPath)。
此函数区分大小写。
示例
以下示例阐释如何使用 starts-with() 函数查询标题以大写字母“W”开头的书籍集合。
XML 文件 (starts-with.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 文件 (starts-with.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="starts-with(title, 'W')">
<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 文件 (starts-with.xml),将产生以下输出:
Weaving Patterns by Weaver costs 150.00.
Writing Style by Writer costs 1500.00.