Re: Re: [xsl] Mutual Recursion with Anonymous (Inline) Functions in XPath 3

Dimitre Novatchev <[email protected]> Thu, 5 Oct 2023 11:19:38 -0700
Newsgroups gmane.text.xml.devel
Message-ID <CAK4KnZf+thovqSDJCqc7sXKTb-3cgJM_BvH8mO7t2h1DJXXZeg@mail.gmail.com>
--00000000000072ae910606fc2b21
Content-Type: text/plain; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable

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=E2=80=AFAM Mukul Gandhi <[email protected]=
rg>
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=E2=80=AFAM Dimitre Novatchev dnovatchev@gmail=
.com <
> [email protected]> wrote:
>
>> Thought this topic might be interesting to some of us:
>>
>>
>> https://medium.com/@dimitrenovatchev/mutual-recursion-with-anonymous-inl=
ine-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 =3D=3D 0)
>         return true;
>     else
>         return is_odd(n - 1);
> }
>
> bool is_odd(unsigned int n) {
>     if (n =3D=3D 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=3D"http://www.w3.org/1999/XSL/Transform"
>                          version=3D"3.0">
>
>   <xsl:output method=3D"xml" indent=3D"yes"/>
>
>   <xsl:variable name=3D"isEven" select=3D"function($n) { if ($n eq 0) the=
n
> true() else $isOdd($n - 1) }"/>
>
>   <xsl:variable name=3D"isOdd" select=3D"function($n) { if ($n eq 0) then
> false() else $isEven($n - 1) }"/>
>
>   <xsl:template match=3D"/">
>      <result>
>          <one>
>               <xsl:value-of select=3D"$isEven(255)"/>
>          </one>
>          <two>
>              <xsl:value-of select=3D"$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=3D"1.0" encoding=3D"UTF-8"?><result>
>     <one>false</one>
>     <two>true</two>
> </result>
>
>
> --
> Regards,
> Mukul Gandhi
>

--00000000000072ae910606fc2b21
Content-Type: text/html; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable

<div dir=3D"ltr"><div dir=3D"ltr">Hi Mukul,<br><br>The problem is to implem=
ent mutual recursion in pure XPath using XPath function items - and to disp=
el=C2=A0the faulty belief that some people share with us, that this is impo=
ssible to do.<div><br></div><div>As for doing this with XSLT - of course, b=
ut notice that you had to maintain the functions in **global** variables in=
 order to achieve this.<br><br>Any pure XPath solution has the added benefi=
t that it can be used in both XSLT and/or XQuery code without the need to c=
hange the XPath expression.<br><br>Thanks,</div><div>Dimitre</div></div><br=
><div class=3D"gmail_quote"><div dir=3D"ltr" class=3D"gmail_attr">On Thu, O=
ct 5, 2023 at 8:43=E2=80=AFAM Mukul Gandhi &lt;<a href=3D"mailto:mukulg@sof=
twarebytes.org">[email protected]</a>&gt; wrote:<br></div><blockquot=
e class=3D"gmail_quote" style=3D"margin:0px 0px 0px 0.8ex;border-left:1px s=
olid rgb(204,204,204);padding-left:1ex"><div dir=3D"ltr"><div>Hi Dimitre,</=
div><div>=C2=A0 =C2=A0 Nice use case you&#39;ve shared for XPath and XSLT. =
I&#39;ve felt like responding to this.</div><br><div class=3D"gmail_quote">=
<div dir=3D"ltr" class=3D"gmail_attr">On Thu, Oct 5, 2023 at 8:27=E2=80=AFA=
M Dimitre Novatchev <a href=3D"mailto:[email protected]" target=3D"_blan=
k">[email protected]</a> &lt;<a href=3D"mailto:[email protected]=
lberrytech.com" target=3D"_blank">[email protected]</=
a>&gt; wrote:<br></div><blockquote class=3D"gmail_quote" style=3D"margin:0p=
x 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><d=
iv dir=3D"ltr">Thought this topic might be interesting to some of us:<br><b=
r><a href=3D"https://medium.com/@dimitrenovatchev/mutual-recursion-with-ano=
nymous-inline-functions-in-xpath-3-0eb7c961806f" target=3D"_blank">https://=
medium.com/@dimitrenovatchev/mutual-recursion-with-anonymous-inline-functio=
ns-in-xpath-3-0eb7c961806f</a><br><br>Also published in my blog.</div></blo=
ckquote><div><br></div><div>I&#39;ve understood the intent of this use case=
, by reading the following C language program that you&#39;ve shared on you=
r blog,</div><div><br></div>bool is_even(unsigned int n) {<br>=C2=A0 =C2=A0=
 if (n =3D=3D 0)<br>=C2=A0 =C2=A0 =C2=A0 =C2=A0 return true;<br>=C2=A0 =C2=
=A0 else<br>=C2=A0 =C2=A0 =C2=A0 =C2=A0 return is_odd(n - 1);<br>}<br><br>b=
ool is_odd(unsigned int n) {<br>=C2=A0 =C2=A0 if (n =3D=3D 0)<br>=C2=A0 =C2=
=A0 =C2=A0 =C2=A0 return false;<br>=C2=A0 =C2=A0 else<br>=C2=A0 =C2=A0 =C2=
=A0 =C2=A0 return is_even(n - 1);<br><div>}</div><div><br></div><div>You wr=
ote that you&#39;ve solved this use case, with pure XPath 3, using Saxon. O=
fcourse, Saxon is expected to be able to solve this for XML technologies,=
=C2=A0since its grately compliant to XSLT 3.0 and XPath 3.1 specs.</div><di=
v><br></div><div>XalanJ currently has an XSLT 3.0 development branch on its=
 codebase repos, where XalanJ team has implemented various XSLT 3.0 and XPa=
th 3.1 language features.</div><div><br></div><div>I wrote the following XS=
LT 3.0 stylesheet to solve this use case (I&#39;ve translated the C program=
, that you&#39;ve cited on your blog and I&#39;ve reproduced that as mentio=
ned above),</div><div><br></div><div>&lt;xsl:stylesheet xmlns:xsl=3D&quot;<=
a href=3D"http://www.w3.org/1999/XSL/Transform" target=3D"_blank">http://ww=
w.w3.org/1999/XSL/Transform</a>&quot;<br>=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0=
 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0version=3D&quot;3.0=
&quot;&gt;<br>=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 <br>=
=C2=A0 &lt;xsl:output method=3D&quot;xml&quot; indent=3D&quot;yes&quot;/&gt=
;<br>=C2=A0 <br>=C2=A0 &lt;xsl:variable name=3D&quot;isEven&quot; select=3D=
&quot;function($n) { if ($n eq 0) then true() else $isOdd($n - 1) }&quot;/&=
gt;<br>=C2=A0 <br>=C2=A0 &lt;xsl:variable name=3D&quot;isOdd&quot; select=
=3D&quot;function($n) { if ($n eq 0) then false() else $isEven($n - 1) }&qu=
ot;/&gt;<br>=C2=A0 <br>=C2=A0 &lt;xsl:template match=3D&quot;/&quot;&gt; =
=C2=A0 =C2=A0 <br>=C2=A0 =C2=A0 =C2=A0&lt;result&gt;<br>=C2=A0 =C2=A0 =C2=
=A0 =C2=A0 =C2=A0&lt;one&gt;<br>=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =
=C2=A0 &lt;xsl:value-of select=3D&quot;$isEven(255)&quot;/&gt;<br>=C2=A0 =
=C2=A0 =C2=A0 =C2=A0 =C2=A0&lt;/one&gt;<br>=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=
=A0&lt;two&gt;<br>=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0&lt;xsl:v=
alue-of select=3D&quot;$isOdd(255)&quot;/&gt;<br>=C2=A0 =C2=A0 =C2=A0 =C2=
=A0 =C2=A0&lt;/two&gt;<br>=C2=A0 =C2=A0 =C2=A0&lt;/result&gt;<br>=C2=A0 &lt=
;/xsl:template&gt;<br><br>&lt;/xsl:stylesheet&gt;<br></div><div><br></div><=
div>Running the above XSLT 3.0 stylesheet with XalanJ&#39;s dev codebase bu=
ild (and with Saxon HE 12.2 as well), produces following expected XSLT tran=
sformation result,</div><div><br></div><div>&lt;?xml version=3D&quot;1.0&qu=
ot; encoding=3D&quot;UTF-8&quot;?&gt;&lt;result&gt;<br>=C2=A0 =C2=A0 &lt;on=
e&gt;false&lt;/one&gt;<br>=C2=A0 =C2=A0 &lt;two&gt;true&lt;/two&gt;<br>&lt;=
/result&gt;<br></div><div>=C2=A0</div></div><div><br></div><span class=3D"g=
mail_signature_prefix">-- </span><br><div dir=3D"ltr" class=3D"gmail_signat=
ure"><div dir=3D"ltr"><div>Regards,<br></div><div>Mukul Gandhi</div></div><=
/div></div>
</blockquote></div><br clear=3D"all"><div><br></div></div>

--00000000000072ae910606fc2b21--