Computing with XSLT
Since XSLT is basically a pattern-matching functional programming language, we should be able to use it to compute. I’m going to use it to implement one of the most basic functions: Peano-style addition.
First of all we have to have an idea of what the numbers look like. We want to simulate this in XSL:
data Nat = Zero | Succ Nat
Using XSD we can have something like this:
<complexType name="Nat"> <choice> <element name="Zero"/> <element name="Succ" type="Nat"/> </choice> </complexType>
(Note that this is not tested, and probably wrong!)
However, since I don’t have an XSLT processor on hand that can deal with XSD types, I had to drop this course of action (although I still used the introduced type for the data). The type gives data like this:
<Succ><Succ><Zero/></Succ></Succ>…in this case standing for the number two. Now the idea of addition on these numbers is given by a function:
add Zero y = y add (Succ x) y = Succ (add x y)
Since this function involves pattern-matching it is an ideal candidate for implementation in XSLT, and it looks something like this:
<?xml version="1.0" encoding="UTF-8"?> <xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <!-- apply the add function to two numbers --> <xsl:template name="add" match="add"> <xsl:apply-templates mode="adder" select="*[1]"> <xsl:with-param name="rest" select="*[2]"/> </xsl:apply-templates> </xsl:template> <!-- add Succ(x) y = Succ(add x y) --> <xsl:template name="addSucc" mode="adder" match="Succ"> <xsl:param name="rest"/> <Succ> <xsl:apply-templates mode="adder"> <xsl:with-param name="rest" select="$rest"/> </xsl:apply-templates> </Succ> </xsl:template> <!-- add Zero y = y --> <xsl:template name="addZero" mode="adder" match="Zero"> <xsl:param name="rest"/> <xsl:copy-of select="$rest"/> </xsl:template> </xsl:stylesheet>
The first template matches any occurances of <add/>, and calls the real ‘add’ function on them. It passes the second number as a parameter, while the first is what the function actually recurses on—exactly as in the Haskell example. The rest proceeds similarly.
You can test it yourself by using the W3C online XSLT processor, which uses my processor and some sample input data. It should work for any similar data that you can throw at it as well. In this case it is adding 2 + 2 and coming up with 4, as we expect

Google “fxsl” …
Hi, I’m searching tutorials about the comparison between the concept of XSLT and functionnal programming. please help me