xml - xslt:无法识别 2 参数函数

标签 xml function xslt

在以下 XSLT 片段中

<?xml version="1.0" ?>
<xsl:stylesheet version="2.0"
            xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
            xmlns:xs="http://www.w3.org/2001/XMLSchema"
            xmlns:my="bla">

    <xsl:template match="/">
       <xsl:value-of select="my:add(4,2)"/>
    </xsl:template>

    <xsl:function name="my:add" as="xs:integer">
        <xs:param name="n" as="xs:integer"/>
        <xs:param name="k" as="xs:integer"/>
        <xsl:value-of select="$n + $k"/>
    </xsl:function>

</xsl:stylesheet>

我收到以下错误:

Static error in {my:add(4,2)} in expression in xsl:value-of/@select on line 9 column 40 of john.xsl:
  XPST0017: Cannot find a 2-argument function named {bla}add(). The namespace URI and local
  name are recognized, but the number of arguments is wrong
Static error at char 3 in xsl:value-of/@select on line 30 column 37 of john.xsl:
  XPST0008: Variable n has not been declared (or its declaration is not in scope)
Errors were reported during stylesheet compilation

我知道我可以使用 <xsl:function name="my:add" as="xs:integer*">作为函数头,但我不想这样。我找不到这有什么问题,因为我发现了几个类似的例子。

最佳答案

函数参数在 Schema 命名空间中。它们需要位于 XSLT 命名空间中。

没有任何xsl:param ,它是一个零元数函数,包含 Schema 命名空间中的两个参数元素。

[Definition: The arity of a stylesheet function is the number of xsl:param elements in the function definition.] Optional arguments are not allowed.

param 元素上将命名空间前缀从 xs 更改为 xsl:xsl:param

此外,由于您的函数返回一个整数,因此请使用 xsl:sequence 而不是 xsl:value-ofxsl:value-of 将从数字结果中生成一个字符串,然后需要将其转换为 xs:integer。只需按原样返回数字产品。

<?xml version="1.0" ?>
<xsl:stylesheet version="2.0"
            xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
            xmlns:xs="http://www.w3.org/2001/XMLSchema"
            xmlns:my="bla">

    <xsl:template match="/">
       <xsl:value-of select="my:add(4,2)"/>
    </xsl:template>

    <xsl:function name="my:add" as="xs:integer">
        <xsl:param name="n" as="xs:integer"/>
        <xsl:param name="k" as="xs:integer"/>
        <xsl:sequence select="$n + $k"/>
    </xsl:function>

</xsl:stylesheet>

关于xml - xslt:无法识别 2 参数函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52465171/

相关文章:

xml - 将多个元素值连接到 xslt 2.0 中的新字符串的最佳方法是什么

PHP XML 验证在传递错误 XML 时抛出致命异常

JavaScript 初学者变量混淆

c# - 使用 XSL 将 XML 转换为 XAML

java - 垂直拆分 Android 布局向两侧添加重力以使对象居中

xml - XSLT - 标签不匹配

php - 如何指定 PHP 中函数的默认参数?

javascript - 排序并返回数组任务中的差异,返回未定义

XSLT:小计

java - 从 XSLT 调用 Java boolean 函数