Thanks Mike for the useful information, I have not much exposure to functional programming and finding the papers refered to in Dimitre's article interesting.

I'm working in XQuery not XSLT (though I understand I can use similar concepts using the saxon:function() call, because I don't think you can pass functions as args in XQuery).

I was able to deal with this using various let/for statements (the general problem I'm solving is making a hierarchy out of a set of flat nodes, where the level in the hierarchy can be dynamically determined).  The naive implementation is O n**2, so I was trying to do something more reasonable, and that's why saxon:leading() looked so attractive.  I think I have accomplished this.

One thing that would really be nice is the notion of stopping a FOR statement when a given condition is found to avoid having to look at everything once you have what you want (this is in essence what saxon:leading() does).

Francis

Michael Kay wrote:
This doesn't look particularly easy to do, given the way saxon:leading is
implemented. It could probably be implemented rather differently, but that's
another matter. I haven't touched saxon:leading() and similar extension
functions for a long time. 

I'm not sure this is really the best way of doing higher-order functions in
XSLT; Dimitre Novatchev's FXSL library is in many ways a more satisfactory
approach, and he has certainly taken the ideas a lot further. If you think
of the second argument to saxon:leading() as a function that takes a single
argument, the item being tested, then what you are trying to do is to
construct a suitable function by supplying arguments to a function with
multiple arguments (unbound variables in your XPath expression). That is,
you're trying to do partial application (or currying) of the function as
described in
http://fxsl.sourceforge.net/articles/PartialApps/Partial%20Applications.html

In functional terms you can define the leading() function using the
recursive definition

leading(SEQ, F) =>

  if (F(SEQ[1])) then (SEQ[1], leading(tail(SEQ), F)) else ()

and I think this would fit very neatly into the FXSL framework. No doubt
Dimitre Novatchev will be happy to help you with the details.

Do you really need the condition to be completely dynamic? If not, you can
implement saxon:leading in XSLT 2.0 as:

<xsl:variable name="leading" as="item()*">
  <xsl:for-each-group select="$in" group-adjacent="CONDITION">
    <xsl:if test="position()=1 and current-grouping-key()">
      <xsl:sequence select="current-group()"/>
    </xsl:if>
  </xsl:for-each-group>
</xsl:variable>

where $in is the input sequence and CONDITION is the condition the items
must satisfy - this can be any boolean expression, and it can of course
include variables.

Or of course you can use a recursive function.

Michael Kay
http://www.saxonica.com/


  
-----Original Message-----
From: saxon-help-bounces@lists.sourceforge.net 
[mailto:saxon-help-bounces@lists.sourceforge.net] On Behalf 
Of Francis Upton
Sent: 28 August 2006 13:31
To: Mailing list for SAXON XSLT queries
Subject: [saxon] use of saxon:leading() function with an 
xpath that containsvariables

Hi,

I want to use the saxon:leading() function, using an XPath 
expression that contains variables.

However, the 2nd arg of saxon:leading() takes a prepared 
expression, which is setup by saxon:expression().  While I 
can use the $px args in saxon:expression(); these are only 
possible if I use saxon:eval() on the expression to execute it.

(none of this is what you don't know).

Is there any way that I can get an expression to 
saxon:leading() that uses variables?  If there isn't, I would 
be grateful for hints about where to modify the source.

Thanks,

Francis

--------------------------------------------------------------
-----------
Using Tomcat but need to do more? Need to support web 
services, security?
Get stuff done quickly with pre-integrated technology to make 
your job easier Download IBM WebSphere Application Server 
v.1.0.1 based on Apache Geronimo
http://sel.as-us.falkag.net/sel?cmd=lnk&kid=120709&bid=263057&
dat=121642
_______________________________________________
saxon-help mailing list
saxon-help@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/saxon-help
    


-------------------------------------------------------------------------
Using Tomcat but need to do more? Need to support web services, security?
Get stuff done quickly with pre-integrated technology to make your job easier
Download IBM WebSphere Application Server v.1.0.1 based on Apache Geronimo
http://sel.as-us.falkag.net/sel?cmd=lnk&kid=120709&bid=263057&dat=121642
_______________________________________________
saxon-help mailing list
saxon-help@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/saxon-help