在以下 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-of
。 xsl: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/