Re: [xsl] Mutual Recursion with Anonymous (Inline) Functions in XPath 3
Mukul Gandhi <[email protected]> Thu, 5 Oct 2023 21:13:07 +0530
| Newsgroups | gmane.text.xml.devel |
|---|---|
| Message-ID | <CANGHzgB+7D5FizEUkWa4akHC5zP=y9kdSykmXUOY1rxQOESYSg@mail.gmail.com> |
--0000000000009e401f0606f9fba2
Content-Type: text/plain; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable
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 [email protected]=
om <
[email protected]> wrote:
> Thought this topic might be interesting to some of us:
>
>
> https://medium.com/@dimitrenovatchev/mutual-recursion-with-anonymous-inli=
ne-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) then
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>
--=20
Regards,
Mukul Gandhi
--0000000000009e401f0606f9fba2
Content-Type: text/html; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable
<div dir=3D"ltr"><div>Hi Dimitre,</div><div>=C2=A0 =C2=A0 Nice use case you=
've shared for XPath and XSLT. I'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=AFAM Dimitre Novatchev <a href=3D"mailto:dn=
[email protected]">[email protected]</a> <<a href=3D"mailto:xsl-list=
[email protected]">[email protected]</a=
>> wrote:<br></div><blockquote class=3D"gmail_quote" style=3D"margin:0px=
0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><di=
v dir=3D"ltr">Thought this topic might be interesting to some of us:<br><br=
><a href=3D"https://medium.com/@dimitrenovatchev/mutual-recursion-with-anon=
ymous-inline-functions-in-xpath-3-0eb7c961806f" target=3D"_blank">https://m=
edium.com/@dimitrenovatchev/mutual-recursion-with-anonymous-inline-function=
s-in-xpath-3-0eb7c961806f</a><br><br>Also published in my blog.</div></bloc=
kquote><div><br></div><div>I've understood the intent of this use case,=
by reading the following C language program that you've shared on your=
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'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've translated the C program=
, that you've cited on your blog and I've reproduced that as mentio=
ned above),</div><div><br></div><div><xsl:stylesheet xmlns:xsl=3D"<=
a href=3D"http://www.w3.org/1999/XSL/Transform">http://www.w3.org/1999/XSL/=
Transform</a>"<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"3.0"><br>=C2=
=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 <br>=C2=A0 <xsl:out=
put method=3D"xml" indent=3D"yes"/><br>=C2=A0 <br>=
=C2=A0 <xsl:variable name=3D"isEven" select=3D"function($=
n) { if ($n eq 0) then true() else $isOdd($n - 1) }"/><br>=C2=A0 <b=
r>=C2=A0 <xsl:variable name=3D"isOdd" select=3D"function(=
$n) { if ($n eq 0) then false() else $isEven($n - 1) }"/><br>=C2=A0=
<br>=C2=A0 <xsl:template match=3D"/"> =C2=A0 =C2=A0 <br>=
=C2=A0 =C2=A0 =C2=A0<result><br>=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0<=
;one><br>=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 <xsl:value-=
of select=3D"$isEven(255)"/><br>=C2=A0 =C2=A0 =C2=A0 =C2=A0 =
=C2=A0</one><br>=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0<two><br>=C2=
=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0<xsl:value-of select=3D&quo=
t;$isOdd(255)"/><br>=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0</two><=
br>=C2=A0 =C2=A0 =C2=A0</result><br>=C2=A0 </xsl:template><br><=
br></xsl:stylesheet><br></div><div><br></div><div>Running the above X=
SLT 3.0 stylesheet with XalanJ's dev codebase build (and with Saxon HE =
12.2 as well), produces following expected XSLT transformation result,</div=
><div><br></div><div><?xml version=3D"1.0" encoding=3D"UT=
F-8"?><result><br>=C2=A0 =C2=A0 <one>false</one><=
br>=C2=A0 =C2=A0 <two>true</two><br></result><br></div><d=
iv>=C2=A0</div></div><div><br></div><span class=3D"gmail_signature_prefix">=
-- </span><br><div dir=3D"ltr" class=3D"gmail_signature"><div dir=3D"ltr"><=
div>Regards,<br></div><div>Mukul Gandhi</div></div></div></div>
--0000000000009e401f0606f9fba2--