Calls to void functions are ignored

Help
Anonymous
2010-07-21
2012-10-08

  • Anonymous
    2010-07-21

    When calling a Java extension function, it seems that calls to void functions
    are ignored by Saxon when the function call is wrapped by an XSL variable like
    such:

                   <xsl:variable name="xx" select="ab:setString($ab,'StringName', NodeValue)"/>
    

    However, the function is invoked if this is changed to:

                   <xsl:value-of select="ab:setString($ab,'StringName', NodeValue)"/>
    

    I did find some others having similar issues.
    http://stackoverflow.com/questions/2631301/cannot-access-updated-java-object-
    from-saxon-xslt-processor

    I am noticing similar behavior with the "document()" function in Saxon as
    well.

    Any insight would be appreciated. Thanks.

     
  • Michael Kay
    Michael Kay
    2010-07-21

    It's nothing to do with the function being void: variables are evaluated on
    first reference, so if you declare a variable but don't use it, Saxon never
    evaluates the variable and therefore never calls the function.

    The advice is to avoid functions with side-effects if you can. But if you
    can't, you need to use them in such a way that the logic of the stylesheet at
    least appears to use the function's result.

     

  • Anonymous
    2010-07-21

    Thank you for your response.

    Is there an alternate syntax that can be used, that guarantees that the
    function is invoked?

    Thanks.

     
  • Michael Kay
    Michael Kay
    2010-07-22

    There's no purely syntactic remedy, for example calling the function within
    xsl:value-of would do no good if the xsl:value-of instruction itself is not
    executed. But you can apply this test: if there's a location in your
    stylesheet where <xsl:text>Hi!</xsl:text> would cause the string Hi! to be
    written to the final result tree, then a call to <xsl:sequence select="f:void- function()"/> in the same position will cause the function to be invoked.
    Similarly if you take two instructions <xsl:text>Hello</xsl:text> and
    <xsl:text>World</xsl:text> and replace them both with function calls, then if
    the order of the output would have been Hello followed by World, you also have
    (for the current release at any rate) a guarantee that the two function calls
    will occur in the "right" order.

    In general though I would emphasize that calling functions with side-effects
    is something to avoid if you can.