以下 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>
这里我直接用XPath String function第 1 行中的 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>
在这里,我使用的是 XPath 函数,其前缀附加到命名空间。
IE 显示“错误:命名空间‘http://www.w3.org/2005/xpath-functions’不包含任何函数” 我检查了 URL,它确实有功能。
我哪里错了?如果我可以将所有 XPath 函数与 Transform URL 本身一起使用,那么为什么要为 XPath 函数提供一个单独的 URL?
最佳答案
Now, as per W3Schools, adding the namespace for XPath functions (http://www.w3.org/2005/xpath-functions), the following code does not work :-
...
Here, I am using the XPath function with its prefix attached to the namespace.
IE shows that "Error: Namespace 'http://www.w3.org/2005/xpath-functions' does not contain any 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 函数添加前缀。
关于xml - 带有 fn 前缀的 XPath 函数 URL 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9782283/