带有 fn 前缀的 XPath 函数 URL 不起作用

whi*_*hat 5 xml xslt xpath

以下 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" />
            &#160; 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" />
            &#160; by 
            <xsl:value-of select="author" />
            <br/>
        </xsl:if>
    </xsl:for-each>
</xsl:template>
</xsl:stylesheet>
Run Code Online (Sandbox Code Playgroud)

在这里,我使用 XPath 函数,并将其前缀附加到命名空间。

IE 显示“错误:命名空间‘ http://www.w3.org/2005/xpath-functions ’不包含任何函数”我检查了 URL,它确实有函数。

我哪里错了?如果我可以将所有 XPath 函数与 Transform URL 本身一起使用,那么为什么要为 XPath 函数提供单独的 URL?

Dim*_*hev 3

现在,根据 W3Schools,添加 XPath 函数的命名空间 (http://www.w3.org/2005/xpath-functions),以下代码不起作用:-

...

在这里,我使用 XPath 函数,并将其前缀附加到命名空间。

IE 显示“错误:命名空间 'http://www.w3.org/2005/xpath-functions' 不包含任何函数”

尽量避免“w3schools”。了解原因: http: //w3fools.com/

您尝试使用的 F & O 命名空间是在 W3C XPath 1.0 和 XSLT 1.0 建议发布多年后创建的。它仅与 XPath 2.0 函数相关,并且 XPath 1.0/XSLT 1.0 处理器不知道此命名空间。

即使使用 XPath 2.0/XSLT 2.0 也不需要使用命名空间—— 任何无前缀的函数名称都被视为在此命名空间中。

解决方案

只需不要为任何标准 XPath 函数添加前缀即可。