Skip to content

Instantly share code, notes, and snippets.

@terremoth
Last active January 11, 2025 17:48
Show Gist options
  • Save terremoth/1f4205bec7369a0a1a4b91b1500344f4 to your computer and use it in GitHub Desktop.
Save terremoth/1f4205bec7369a0a1a4b91b1500344f4 to your computer and use it in GitHub Desktop.
XSLT + XML are turing complete
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<html>
<head>
<title>Fibonacci Sequence</title>
</head>
<body>
<h1>Fibonacci Sequence</h1>
<p>
<xsl:call-template name="fibonacci">
<xsl:with-param name="n" select="/input/@n" />
</xsl:call-template>
</p>
</body>
</html>
</xsl:template>
<xsl:template name="fibonacci">
<xsl:param name="n" />
<xsl:param name="prev" select="0" />
<xsl:param name="curr" select="1" />
<xsl:if test="$n > 0">
<xsl:value-of select="$curr" />
<xsl:text>, </xsl:text>
<xsl:call-template name="fibonacci">
<xsl:with-param name="n" select="$n - 1" />
<xsl:with-param name="prev" select="$curr" />
<xsl:with-param name="curr" select="$prev + $curr" />
</xsl:call-template>
</xsl:if>
</xsl:template>
</xsl:stylesheet>
<?xml version="1.0"?>
<?xml-stylesheet type="text/xsl" href="fibonacci.xsl"?>
<input n="10"/>
@terremoth
Copy link
Author

XSLT + XML are turing complete

Open the input.xml in your browser

Note: you will have to open it with a web server, not just opening "as a file", that won't work

image

You can see the XSLT solving a fibonacci sequence passed in input.xml

Sadly, there is no way to make it "dynamic", neither using HTTP/GET, yet, it is possible to compute things with both

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment