以下 XLST 代码工作正常:-
<?xml version="1.0"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0">
<xsl:template match="/">
<xsl:for-each select="bookstore/book">
<xsl:if test="starts-with(author, 'W')"> <!-- Line 1 -->
<xsl:value-of select="title" />
  by
<xsl:value-of select="author" />
<br/>
</xsl:if>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
Run Code Online (Sandbox Code Playgroud)
这里我直接使用第 1 行中的XPath String 函数starts-with()。
现在,根据W3Schools,添加 XPath 函数的命名空间 ( http://www.w3.org/2005/xpath-functions ),以下代码不起作用:-
<?xml version="1.0"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:fn="http://www.w3.org/2005/xpath-functions" version="1.0">
<xsl:template match="/">
<xsl:for-each select="bookstore/book">
<xsl:if test="fn:starts-with(author, 'W')"> <!-- Line 2 -->
<xsl:value-of select="title" />
  by
<xsl:value-of select="author" />
<br/>
</xsl:if>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
Run Code Online (Sandbox Code Playgroud)
在这里,我使用 XPath 函数,并将其前缀附加到命名空间。 …