<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>porges &#187; XSLT</title>
	<atom:link href="http://porg.es/blog/tag/xslt/feed" rel="self" type="application/rss+xml" />
	<link>http://porg.es/blog</link>
	<description></description>
	<lastBuildDate>Thu, 12 Jan 2012 23:45:56 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Computing with XSLT</title>
		<link>http://porg.es/blog/computing-with-xslt</link>
		<comments>http://porg.es/blog/computing-with-xslt#comments</comments>
		<pubDate>Thu, 19 Jul 2007 02:34:18 +0000</pubDate>
		<dc:creator>Porges</dc:creator>
				<category><![CDATA[Cool]]></category>
		<category><![CDATA[Functional programming]]></category>
		<category><![CDATA[Haskell]]></category>
		<category><![CDATA[Mathematics]]></category>
		<category><![CDATA[XML]]></category>
		<category><![CDATA[XSLT]]></category>

		<guid isPermaLink="false">http://porg.es/blog/computing-with-xslt</guid>
		<description><![CDATA[Since XSLT is basically a pattern-matching functional programming language, we should be able to use it to compute. I&#8217;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: [...]]]></description>
			<content:encoded><![CDATA[<p>Since XSLT is basically a pattern-matching functional programming language, we should be able to use it to compute. I&#8217;m going to use it to implement one of the most basic functions: Peano-style addition.</p>
<p>First of all we have to have an idea of what the numbers look like. We want to simulate this in XSL:</p>

<div class="wp_syntax"><div class="code"><pre class="haskell" style="font-family:monospace;"><span style="color: #06c; font-weight: bold;">data</span> Nat <span style="color: #339933; font-weight: bold;">=</span> Zero <span style="color: #339933; font-weight: bold;">|</span> Succ Nat</pre></div></div>

<p>Using XSD we can have something like this:</p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;complexType</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;Nat&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;choice<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;element</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;Zero&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;element</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;Succ&quot;</span> <span style="color: #000066;">type</span>=<span style="color: #ff0000;">&quot;Nat&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/choice<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/complexType<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div>

<p><small>(Note that this is not tested, and probably wrong!)</small></p>
<p>However, since I don&#8217;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:</p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;Succ<span style="color: #000000; font-weight: bold;">&gt;</span></span><span style="color: #000000; font-weight: bold;">&lt;Succ<span style="color: #000000; font-weight: bold;">&gt;</span></span><span style="color: #000000; font-weight: bold;">&lt;Zero</span><span style="color: #000000; font-weight: bold;">/&gt;</span><span style="color: #000000; font-weight: bold;">&lt;/Succ<span style="color: #000000; font-weight: bold;">&gt;</span></span><span style="color: #000000; font-weight: bold;">&lt;/Succ<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div>

<p>&#8230;in this case standing for the number two. Now the idea of addition on these numbers is given by a function:</p>

<div class="wp_syntax"><div class="code"><pre class="haskell" style="font-family:monospace;">add Zero y <span style="color: #339933; font-weight: bold;">=</span> y
add <span style="color: green;">&#40;</span>Succ x<span style="color: green;">&#41;</span> y <span style="color: #339933; font-weight: bold;">=</span> Succ <span style="color: green;">&#40;</span>add x y<span style="color: green;">&#41;</span></pre></div></div>

<p>Since this function involves pattern-matching it is an ideal candidate for implementation in XSLT, and it looks something like this:</p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;?xml</span> <span style="color: #000066;">version</span>=<span style="color: #ff0000;">&quot;1.0&quot;</span> <span style="color: #000066;">encoding</span>=<span style="color: #ff0000;">&quot;UTF-8&quot;</span><span style="color: #000000; font-weight: bold;">?&gt;</span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;xsl:stylesheet</span> <span style="color: #000066;">version</span>=<span style="color: #ff0000;">&quot;2.0&quot;</span> </span>
<span style="color: #009900;"><span style="color: #000066;">xmlns:xsl</span>=<span style="color: #ff0000;">&quot;http://www.w3.org/1999/XSL/Transform&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
&nbsp;
	<span style="color: #808080; font-style: italic;">&lt;!-- apply the add function to two numbers --&gt;</span>
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;xsl:template</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;add&quot;</span> <span style="color: #000066;">match</span>=<span style="color: #ff0000;">&quot;add&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
		<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;xsl:apply-templates</span> <span style="color: #000066;">mode</span>=<span style="color: #ff0000;">&quot;adder&quot;</span> <span style="color: #000066;">select</span>=<span style="color: #ff0000;">&quot;*[1]&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
			<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;xsl:with-param</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;rest&quot;</span> <span style="color: #000066;">select</span>=<span style="color: #ff0000;">&quot;*[2]&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
		<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/xsl:apply-templates<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/xsl:template<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
&nbsp;
	<span style="color: #808080; font-style: italic;">&lt;!-- add Succ(x) y = Succ(add x y) --&gt;</span>
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;xsl:template</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;addSucc&quot;</span> <span style="color: #000066;">mode</span>=<span style="color: #ff0000;">&quot;adder&quot;</span> <span style="color: #000066;">match</span>=<span style="color: #ff0000;">&quot;Succ&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
		<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;xsl:param</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;rest&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
		<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;Succ<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
			<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;xsl:apply-templates</span> <span style="color: #000066;">mode</span>=<span style="color: #ff0000;">&quot;adder&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
				<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;xsl:with-param</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;rest&quot;</span> <span style="color: #000066;">select</span>=<span style="color: #ff0000;">&quot;$rest&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
			<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/xsl:apply-templates<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
		<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/Succ<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/xsl:template<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
&nbsp;
	<span style="color: #808080; font-style: italic;">&lt;!-- add Zero y = y --&gt;</span>
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;xsl:template</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;addZero&quot;</span> <span style="color: #000066;">mode</span>=<span style="color: #ff0000;">&quot;adder&quot;</span> <span style="color: #000066;">match</span>=<span style="color: #ff0000;">&quot;Zero&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
		<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;xsl:param</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;rest&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
		<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;xsl:copy-of</span> <span style="color: #000066;">select</span>=<span style="color: #ff0000;">&quot;$rest&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/xsl:template<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
&nbsp;
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/xsl:stylesheet<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div>

<p>The first template matches any occurances of <code>&lt;add/&gt;</code>, and calls the real &#8216;add&#8217; function on them. It passes the second number as a parameter, while the first is what the function actually recurses on&mdash;exactly as in the Haskell example. The rest proceeds similarly.</p>
<p>You can <a href="http://www.w3.org/2005/08/online_xslt/xslt?xslfile=http%3A%2F%2Fporg.es%2Fprocessor.xsl&#038;xmlfile=http%3A%2F%2Fporg.es%2Fdata.xml&#038;content-type=&#038;submit=transform">test it yourself</a> by using the W3C online XSLT processor, which uses <a href="http://porg.es/processor.xsl">my processor</a> and <a href="http://porg.es/data.xml">some sample input data</a>. 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 <img src="http://porg.es/blog/wp-content/plugins/wp-smiley-switcher/noktahhitam/icon_smile.gif" alt="" /></p>
]]></content:encoded>
			<wfw:commentRss>http://porg.es/blog/computing-with-xslt/feed</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>

