<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                xmlns="http://www.w3.org/2000/svg"
                version="1.0">
  <xsl:variable name="width" select="/options/width"/>
  <xsl:variable name="height" select="/options/height"/>
  <xsl:variable name="recursedepth" select="/options/depth"/>
  <xsl:variable name="left" select="($width div 2) - $width"/>
  <xsl:variable name="top" select="($height div 2) - $width"/>
  <xsl:variable name="right" select="0 - $left"/>
  <xsl:variable name="bottom" select="0 - $top"/>
  <xsl:variable name="stroke" select="/options/strokewidth"/>

  <xsl:template match="/">  	
    <svg viewBox="{$left} {$top} {$width} {$height}">
      <xsl:call-template name="draw-primitive">
        <xsl:with-param name="depth" select="$recursedepth"/>
      </xsl:call-template>
    </svg>
  </xsl:template>

  <xsl:template name="draw-primitive">
    <xsl:param name="depth" select="0"/>

    <xsl:if test="$depth > 0">
      <xsl:variable name="localstroke" select="$stroke * ($recursedepth - $depth)"/>
      <polyline points="{$right},{$bottom} 0,{$top} {$left},{$bottom} {$right},{$bottom}" stroke="black" stroke-width="{$localstroke}" fill="white"/>
      <g transform="scale(.5,.5)">
        <g transform="translate(0,{$top})">
          <xsl:call-template name="draw-primitive">
            <xsl:with-param name="depth" select="$depth - 1"/>
          </xsl:call-template>            
        </g>
        
        <g transform="translate({$left},{$bottom})">
          <xsl:call-template name="draw-primitive">
            <xsl:with-param name="depth" select="$depth - 1"/>
          </xsl:call-template>            
        </g>
        
        <g transform="translate({$right},{$bottom})">
          <xsl:call-template name="draw-primitive">
            <xsl:with-param name="depth" select="$depth - 1"/>
          </xsl:call-template>            
        </g>
      </g>

    </xsl:if>
  </xsl:template>
</xsl:stylesheet>

