<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  xmlns="http://www.w3.org/2000/svg">

  <xsl:output indent="yes"/>

  <xsl:template match="/">
    <svg width="800" height="400" viewBox="0 150 800 300">
      
      <xsl:call-template name="defs"/>
      
      <rect width="800" height="600" fill="burlywood"/>
      <rect width="800" height="300" fill="slateblue" fill-opacity="0.3"/>
      <rect width="400" height="600" fill="indianred" fill-opacity="0.3"/>
      
      <g transform="translate(400 300)">
        <xsl:call-template name="biplane"/>

        <xsl:apply-templates/>
      </g>

    </svg>
  </xsl:template>
  
  <xsl:template name="defs">
    <defs>
      <style type="text/css">
text  { font-size: 6.5pt;
        font-family:"Palatino Linotype" }
      </style>
    </defs>
  </xsl:template>

  <xsl:template name="biplane">
    <circle r="80" fill="gainsboro" fill-opacity="0.4"/>
    <path class="wing" d="M -350 -100 h 700 M -350 100 h 700"
      stroke="darkred" stroke-width="5"/>
  </xsl:template>
  
  <xsl:template match="Octave">
    <xsl:variable name="flip">
      <xsl:choose>
        <xsl:when test="following::Octave">-1</xsl:when>
        <xsl:otherwise>1</xsl:otherwise>
      </xsl:choose>
    </xsl:variable>
    <g class="{local-name()}" transform="translate(0 {100 * $flip})">
      <xsl:apply-templates/>
    </g>
  </xsl:template>

  <xsl:template match="Quatrain">
    <xsl:variable name="flip">
      <xsl:choose>
        <xsl:when test="count(preceding::Quatrain) mod 2">1</xsl:when>
        <xsl:otherwise>-1</xsl:otherwise>
      </xsl:choose>
    </xsl:variable>
    <g class="{local-name()}" transform="translate({190 * $flip} 0)">
      <xsl:apply-templates/>
    </g>
  </xsl:template>

  <xsl:template match="Couplet">
    <xsl:variable name="flip">
      <xsl:choose>
        <xsl:when test="count(preceding-sibling::Couplet) mod 2">-1</xsl:when>
        <xsl:otherwise>1</xsl:otherwise>
      </xsl:choose>
    </xsl:variable>
    <g class="{local-name()}" transform="translate(0 {(-15 * $flip) + 5})">
      <xsl:apply-templates/>
    </g>
  </xsl:template>

  <xsl:template match="Line">
    <xsl:variable name="flip">
      <xsl:choose>
        <xsl:when test="count(preceding-sibling::Line) mod 2">-1</xsl:when>
        <xsl:otherwise>1</xsl:otherwise>
      </xsl:choose>
    </xsl:variable>
    <xsl:variable name="anchor">
      <xsl:choose>
        <xsl:when test="preceding-sibling::Line">start</xsl:when>
        <xsl:otherwise>end</xsl:otherwise>
      </xsl:choose>
    </xsl:variable>
    <g class="{local-name()}" transform="translate({-5 * $flip} 0)"
      text-anchor="{$anchor}">
      <text>
        <xsl:apply-templates/>
      </text>
    </g>
  </xsl:template>

</xsl:stylesheet>
