Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

xslt: 2-argument function not recognized

Tags:

function

xml

xslt

In the following XSLT snippet

<?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>

I get the following errors:

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

I know I could use <xsl:function name="my:add" as="xs:integer*"> as the function head but I do not want to have it this way. I can not find out what is wrong with this because I found several similar examples like this.

like image 409
Äxel Avatar asked Dec 05 '25 13:12

Äxel


1 Answers

The function parameters are in the Schema namespace. They need to be in the XSLT namespace.

Without any xsl:param, it is a zero arity function that contains two param elements that are in the Schema namespace.

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

Change the namespace prefix from xs to xsl on your param elements: xsl:param.

Also, since your function is returning an integer, use xsl:sequence instead of xsl:value-of. xsl:value-of will produce a string from the numeric result, which will then need to be cast to an xs:integer. Just return the numeric product as-is.

<?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>
like image 96
Mads Hansen Avatar answered Dec 07 '25 03:12

Mads Hansen



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!