XML.orgXML.org
FOCUS AREAS |XML-DEV |XML.org DAILY NEWSLINK |REGISTRY |RESOURCES |ABOUT
OASIS Mailing List ArchivesView the OASIS mailing list archive below
or browse/search using MarkMail.

 


Help: OASIS Mailing Lists Help | MarkMail Help

[Date Prev] | [Thread Prev] | [Thread Next] | [Date Next] -- [Date Index] | [Thread Index]
Re: [xml-dev] Re: [xsl] Mutual Recursion with Anonymous (Inline)Functions in XPath 3

Hi Mukul,

The problem is to implement mutual recursion in pure XPath using XPath function items - and to dispel the faulty belief that some people share with us, that this is impossible to do.

As for doing this with XSLT - of course, but notice that you had to maintain the functions in **global** variables in order to achieve this.

Any pure XPath solution has the added benefit that it can be used in both XSLT and/or XQuery code without the need to change the XPath expression.

Thanks,
Dimitre

On Thu, Oct 5, 2023 at 8:43 AM Mukul Gandhi <mukulg@softwarebytes.org> wrote:
Hi Dimitre,
    Nice use case you've shared for XPath and XSLT. I've felt like responding to this.

On Thu, Oct 5, 2023 at 8:27 AM Dimitre Novatchev dnovatchev@gmail.com <xsl-list-service@lists.mulberrytech.com> wrote:
Thought this topic might be interesting to some of us:

https://medium.com/@dimitrenovatchev/mutual-recursion-with-anonymous-inline-functions-in-xpath-3-0eb7c961806f

Also published in my blog.

I've understood the intent of this use case, by reading the following C language program that you've shared on your blog,

bool is_even(unsigned int n) {
    if (n == 0)
        return true;
    else
        return is_odd(n - 1);
}

bool is_odd(unsigned int n) {
    if (n == 0)
        return false;
    else
        return is_even(n - 1);
}

You wrote that you've solved this use case, with pure XPath 3, using Saxon. Ofcourse, Saxon is expected to be able to solve this for XML technologies, since its grately compliant to XSLT 3.0 and XPath 3.1 specs.

XalanJ currently has an XSLT 3.0 development branch on its codebase repos, where XalanJ team has implemented various XSLT 3.0 and XPath 3.1 language features.

I wrote the following XSLT 3.0 stylesheet to solve this use case (I've translated the C program, that you've cited on your blog and I've reproduced that as mentioned above),

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                         version="3.0">
               
  <xsl:output method="xml" indent="yes"/>
 
  <xsl:variable name="isEven" select="function($n) { if ($n eq 0) then true() else $isOdd($n - 1) }"/>
 
  <xsl:variable name="isOdd" select="function($n) { if ($n eq 0) then false() else $isEven($n - 1) }"/>
 
  <xsl:template match="/">    
     <result>
         <one>
              <xsl:value-of select="$isEven(255)"/>
         </one>
         <two>
             <xsl:value-of select="$isOdd(255)"/>
         </two>
     </result>
  </xsl:template>

</xsl:stylesheet>

Running the above XSLT 3.0 stylesheet with XalanJ's dev codebase build (and with Saxon HE 12.2 as well), produces following expected XSLT transformation result,

<?xml version="1.0" encoding="UTF-8"?><result>
    <one>false</one>
    <two>true</two>
</result>
 

--
Regards,
Mukul Gandhi




[Date Prev] | [Thread Prev] | [Thread Next] | [Date Next] -- [Date Index] | [Thread Index]


News | XML in Industry | Calendar | XML Registry
Marketplace | Resources | MyXML.org | Sponsors | Privacy Statement

Copyright 1993-2007 XML.org. This site is hosted by OASIS