<?xml version="1.0" encoding="utf-8"?>

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:fo="http://www.w3.org/1999/XSL/Format">
  <xsl:output method="xml" indent="yes"/>

  <xsl:template match="/">
    <fo:root>

      <fo:layout-master-set>
        <fo:simple-page-master master-name="A4-portrait"
              page-height="29.7cm" page-width="21.0cm" margin="2cm">
          <fo:region-body/>
        </fo:simple-page-master>
      </fo:layout-master-set>

      <fo:page-sequence master-reference="A4-portrait">
        <fo:flow flow-name="xsl-region-body">
          <fo:block>
            <xsl:apply-templates select="/section"/>
          </fo:block>
        </fo:flow>
      </fo:page-sequence>

    </fo:root>
  </xsl:template>


  <xsl:template match="/section">
    <xsl:apply-templates select="processing-instruction('XTC')"/>
  </xsl:template>

  <xsl:template match="processing-instruction('XTC')">
    <fo:block>
      <xsl:call-template name="txtdiff">
        <xsl:with-param name="txt" select="following-sibling::*[1]"/>
      </xsl:call-template>
    </fo:block>
  </xsl:template>

  <xsl:template name="txtdiff">
    <xsl:param name="txt"/>
    <xsl:choose>

      <xsl:when test="starts-with($txt,'[-]')">
        <fo:inline color="red" text-decoration="line-through">
          <xsl:value-of select="substring-after(substring-before($txt, '[/-]'), '[-]')"/>
        </fo:inline>
        <xsl:call-template name="txtdiff">
          <xsl:with-param name="txt" select="substring-after($txt,'[/-]')"/>
        </xsl:call-template>
      </xsl:when>

      <xsl:when test="starts-with($txt,'[+]')">
        <fo:inline color="green">
          <xsl:value-of select="substring-after(substring-before($txt, '[/+]'), '[+]')"/>
        </fo:inline>
        <xsl:call-template name="txtdiff">
          <xsl:with-param name="txt" select="substring-after($txt,'[/+]')"/>
        </xsl:call-template>
      </xsl:when>

      <xsl:otherwise>
        <xsl:choose>
          <xsl:when test="contains($txt, '[-]')">
            <xsl:if test="contains(substring-before($txt, '[-]'), '[+]')">
              <xsl:value-of select="substring-before($txt, '[+]')"/>
              <xsl:call-template name="txtdiff">
                <xsl:with-param name="txt" select="concat('[+]',substring-after($txt,'[+]'))"/>
              </xsl:call-template>
            </xsl:if>
            <xsl:value-of select="substring-before($txt, '[-]')"/>
              <xsl:call-template name="txtdiff">
                <xsl:with-param name="txt" select="concat('[-]',substring-after($txt,'[-]'))"/>
              </xsl:call-template>
          </xsl:when>
          <xsl:when test="contains($txt, '[+]')">
            <xsl:value-of select="substring-before($txt, '[+]')"/>
              <xsl:call-template name="txtdiff">
                <xsl:with-param name="txt" select="concat('[+]',substring-after($txt,'[+]'))"/>
              </xsl:call-template>
          </xsl:when>
          <xsl:otherwise>
            <xsl:value-of select="$txt"/>
          </xsl:otherwise>
        </xsl:choose>
      </xsl:otherwise>

    </xsl:choose>
  </xsl:template>

</xsl:stylesheet>
