Project

Profile

Help

Possible bug in assigning anonymous functions in XSLT 3.0

Added by Nick Nunes over 11 years ago

Using Saxon 9.4.0.4 (bundled with Oxygen 14.1) this stylesheet successfully returns a the expected result (12).


  
  
  
  
    
  
  

I believe it should error due to $gcd not being in scope. Am I incorrect?


Replies (6)

Please register to reply

RE: Possible bug in assigning anonymous functions in XSLT 3.0 - Added by Michael Kay over 11 years ago

I can't see what's wrong with it - $gcd is a global variable, so it is in scope throughout the stylesheet.

Note that although the function has no name, it can be bound to a variable, and the variable always has a name.

Michael Kay Saxonica

RE: Possible bug in assigning anonymous functions in XSLT 3.0 - Added by Nick Nunes over 11 years ago

Sorry I should have included this information that made me suspect this was an error in the initial message. http://www.w3.org/TR/xslt-30/#scope-of-variables says "A global variable binding element is visible everywhere in the stylesheet (including other stylesheet modules) except within the xsl:variable or xsl:param element itself and any region where it is shadowed by another variable binding." From my understanding of that the global should not be in scope within it's own definition, unless shadowing is coming into play here in some way that I don't understand.

RE: Possible bug in assigning anonymous functions in XSLT 3.0 - Added by Michael Kay over 11 years ago

Ah, I see what you mean. Yes, it shouldn't be in scope. Will investigate.

RE: Possible bug in assigning anonymous functions in XSLT 3.0 - Added by Michael Kay over 11 years ago

I think that what is happening here is that Saxon relies on the circularity check to prevent a global variable being defined in terms of itself. In this case the circularity check doesn't fail, because it's quite possible to evaluate the global variable without knowing its own value.

Since what you are doing actually seems to be useful, I'm wondering about proposing a spec change to allow it.

RE: Possible bug in assigning anonymous functions in XSLT 3.0 - Added by Nick Nunes over 11 years ago

Would the proposed change be to the XSLT spec or the XPath spec? I came up with this example while playing around with Dimitre's example of XPath recursion here http://dnovatchev.wordpress.com/2012/10/15/recursion-with-anonymous-inline-functions-in-xpath-3-0-2/.

Possibly related, this variable assignment

let
  $gcd := function($x as xs:integer, $y as xs:integer) {
    if ($y eq 0)
    then abs($x)
    else $gcd($y,$x mod $y)
  }

fails in XQuery 3.0 due to circular dependency. By my understanding of this http://www.w3.org/TR/2013/CR-xquery-30-20130108/#dt-in-scope-variables, and http://www.w3.org/TR/2013/CR-xpath-30-20130108/#dt-in-scope-variables "An expression that binds a variable extends the in-scope variables, within the scope of the variable, with the variable and its type." I would expect it to work there.

RE: Possible bug in assigning anonymous functions in XSLT 3.0 - Added by Michael Kay over 11 years ago

The rule is in XSLT, so it would be an XSLT change.

It's much more difficult to do this trick with local variables, since the local variable has to become part of the function's closure. With global variables one can rely on immutability and only carry a reference to the variable, rather than some representation of its value.

    (1-6/6)

    Please register to reply