Welcome to the third installment of stupid coding tricks! As we learned from the T-SQL Mandelbrot and A Batch of Pi, stupid coding tricks aren't really about obfuscation per se... just, well, stupid awesomeness. Kinda like a quine, except even more useless.Got a trick of your own? I'd love to see it, so send it on in.

If you were to use The Daily WTF as a guide, your impression of XSL Transformations (XSLT) would probably be fairly low. I mean, seeing article after article after article might have given the impression that XSLT is often not the right tool for the job... or, perhaps, maybe not even a right tool. Period.

But regardless of your opinions on XSLT, I'm certain you'll have a new appreciation after seeing what magic it can work on a boring old piece of XML. Even if its variables are write-once and cannot vary.

<?xml version="1.0" encoding="UTF-8"?>
<!-- XSLT Mandelbrot - written by Joel Yliluoma 2007, http://iki.fi/bisqwit/ -->
<?xml-stylesheet type='text/xsl' href='mandelbrot.xsl'?>
<fractal>
 <scale>100</scale>
 <y><min>-120</min> <max>120</max> <step>3.9</step></y>
 <x><min>-203</min> <max>100</max> <step>1.4</step></x>
 <maxiter>28</maxiter>
 
 <background>#500</background>
 
 <magnitude value="0"><symbol>&#9617;</symbol><color>#115</color></magnitude>
 <magnitude value="1"><symbol>&#9617;</symbol><color>#228</color></magnitude>
 <magnitude value="2"><symbol>&#9617;</symbol><color>#22B</color></magnitude>
 <magnitude value="3"><symbol>&#9617;</symbol><color>#33D</color></magnitude>
 <magnitude value="4"><symbol>&#9617;</symbol><color>#44F</color></magnitude>
 <magnitude value="5"><symbol>&#9618;</symbol><color>#55C</color></magnitude>
 <magnitude value="6"><symbol>&#9618;</symbol><color>#55D</color></magnitude>
 <magnitude value="7"><symbol>&#9618;</symbol><color>#55E</color></magnitude>
 <magnitude value="8"><symbol>&#9619;</symbol><color>#55F</color></magnitude>
 <magnitude value="9"><symbol>&#9619;</symbol><color>#66F</color></magnitude>
 <magnitude value="10"><symbol>&#9619;</symbol><color>#77F</color></magnitude>
 <magnitude value="11"><symbol>&#9619;</symbol><color>#88F</color></magnitude>
 <magnitude value="12"><symbol>&#9608;</symbol><color>#88F</color></magnitude>
 <magnitude value="13"><symbol>&#9619;</symbol><color>#99F</color></magnitude>
 <magnitude value="14"><symbol>&#9608;</symbol><color>#99F</color></magnitude>
 <magnitude value="15"><symbol>&#9619;</symbol><color>#AAF</color></magnitude>
 <magnitude value="16"><symbol>&#9608;</symbol><color>#AAF</color></magnitude>
 <magnitude value="17"><symbol>&#9619;</symbol><color>#BBF</color></magnitude>
 <magnitude value="18"><symbol>&#9608;</symbol><color>#BBF</color></magnitude>
 <magnitude value="19"><symbol>&#9619;</symbol><color>#CCF</color></magnitude>
 <magnitude value="20"><symbol>&#9608;</symbol><color>#CCF</color></magnitude>
 <magnitude value="21"><symbol>&#9619;</symbol><color>#DDF</color></magnitude>
 <magnitude value="22"><symbol>&#9608;</symbol><color>#DDF</color></magnitude>
 <magnitude value="23"><symbol>&#9619;</symbol><color>#EEF</color></magnitude>
 <magnitude value="24"><symbol>&#9608;</symbol><color>#EEF</color></magnitude>
 <magnitude value="25"><symbol>&#9619;</symbol><color>#FFF</color></magnitude>
 <magnitude value="26"><symbol>&#9608;</symbol><color>#FFF</color></magnitude>
 <magnitude value="27"><symbol>&#9617;</symbol><color>#000</color></magnitude>
</fractal>

Simple XML, right? Now let's apply a little templating...

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<!-- XSLT Mandelbrot - written by Joel Yliluoma 2007, http://iki.fi/bisqwit/ -->

<xsl:output method="html" indent="no"
  doctype-public="-//W3C//DTD HTML 4.01//EN"
  doctype-system="http://www.w3.org/TR/REC-html40/strict.dtd"
 />

<xsl:template match="/fractal">
 <html>
  <head>
   <title>XSLT fractal</title>
   <style type="text/css">
body { color:#55F; background:#000 }
pre { font-family:monospace; font-size:7px }
pre span { background:<xsl:value-of select="background" /> }
   </style>
  </head>
  <body>
   <div style="position:absolute;top:20px;left:20em">
    Copyright © 1992,2007 Joel Yliluoma
    (<a href="http://iki.fi/bisqwit/">http://iki.fi/bisqwit/</a>)
   </div>
   <h1 style="margin:0px">XSLT fractal</h1>
   <pre><xsl:call-template name="bisqwit-mandelbrot" /></pre>
  </body>
 </html>
</xsl:template>

<xsl:template name="bisqwit-mandelbrot"
  ><xsl:call-template name="bisqwit-mandelbrot-line">
   <xsl:with-param name="y" select="y/min"/>
  </xsl:call-template
></xsl:template>

<xsl:template name="bisqwit-mandelbrot-line"
 ><xsl:param name="y"
 /><xsl:call-template name="bisqwit-mandelbrot-column">
  <xsl:with-param name="x" select="x/min"/>
  <xsl:with-param name="y" select="$y"/>
 </xsl:call-template
 ><xsl:if test="$y < y/max"
  ><br
  /><xsl:call-template name="bisqwit-mandelbrot-line">
   <xsl:with-param name="y" select="$y + y/step"/>
  </xsl:call-template
 ></xsl:if
></xsl:template>

<xsl:template name="bisqwit-mandelbrot-column"
 ><xsl:param name="x"
 /><xsl:param name="y"
 /><xsl:call-template name="bisqwit-mandelbrot-slot">
  <xsl:with-param name="x" select="$x" />
  <xsl:with-param name="y" select="$y" />
  <xsl:with-param name="zr" select="$x" />
  <xsl:with-param name="zi" select="$y" />
 </xsl:call-template
 ><xsl:if test="$x < x/max"
  ><xsl:call-template name="bisqwit-mandelbrot-column">
   <xsl:with-param name="x" select="$x + x/step"/>
   <xsl:with-param name="y" select="$y" />
  </xsl:call-template
 ></xsl:if
></xsl:template>

<xsl:template name="bisqwit-mandelbrot-slot"
><xsl:param name="x"
 /><xsl:param name="y"
 /><xsl:param name="zr"
 /><xsl:param name="zi"
 /><xsl:param name="iter" select="0"
 /><xsl:variable name="zrsqr" select="($zr * $zr)"
 /><xsl:variable name="zisqr" select="($zi * $zi)"
 /><xsl:choose>
  <xsl:when test="(4*scale*scale >= $zrsqr + $zisqr) and (maxiter > $iter+1)"
   ><xsl:call-template name="bisqwit-mandelbrot-slot">
    <xsl:with-param name="x" select="$x" />
    <xsl:with-param name="y" select="$y" />
    <xsl:with-param name="zi" select="(2 * $zr * $zi) div scale + $y" />
    <xsl:with-param name="zr" select="($zrsqr - $zisqr) div scale + $x" />
    <xsl:with-param name="iter" select="$iter + 1" />
   </xsl:call-template
  ></xsl:when>
  <xsl:otherwise
   ><xsl:variable name="magnitude" select="magnitude[@value=$iter]"
    /><span style="color:{$magnitude/color}"
   ><xsl:value-of select="$magnitude/symbol"
  /></span></xsl:otherwise>
 </xsl:choose
></xsl:template>
 
</xsl:stylesheet>

Open it in a browser (go ahead, try it yourself), and voilà! You get XSLT Mandelbrot.

 

 

[Advertisement] BuildMaster allows you to create a self-service release management platform that allows different teams to manage their applications. Explore how!