[XSL-LIST Mailing List Archive Home] [By Thread] [By Date]

RE: [xsl] XPath 3.0: Is it possible to do recursion in an anonymous function?


Subject: RE: [xsl] XPath 3.0: Is it possible to do recursion in an anonymous function?
From: "Costello, Roger L." <costello@xxxxxxxxx>
Date: Sun, 14 Oct 2012 16:44:01 +0000

Thank you very much Dimitre. That is a fantastic technique.

Using Dimitre's technique, I implemented the "until" function:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                            xmlns:xs="http://www.w3.org/2001/XMLSchema"
                            version="3.0">

    <xsl:output method="text"/>

    <xsl:variable name="isNegative" select="function($x as xs:integer) {$x lt
0}" />
    <xsl:variable name="decrement" select="function($x as xs:integer) {$x -
1}" />
    <xsl:variable name="_until" select="
                  function(
                                    $p as function(item()*) as xs:boolean,
                                    $f as function(item()*) as item()*,
                                    $x as item()*,
                                    $_until as function(function(),
function(), item()*, function()) as item()*
                                 ) as item()*
                     {if ($p($x)) then $x else $_until($p, $f, $f($x),
$_until)}
        " />

    <xsl:variable name="until" select="
                  function(
                                     $p as function(item()*) as xs:boolean,
                                     $f as function(item()*) as item()*,
                                     $x as item()*
                                  ) as item()*
                      {$_until($p, $f, $x, $_until)}
          " />

    <xsl:template match="/">
        <xsl:value-of select="$until($isNegative, $decrement, 3)" />
    </xsl:template>

</xsl:stylesheet>

/Roger


Current Thread