<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:fo="http://www.w3.org/1999/XSL/Format" version="1.0">
    <xsl:template match="direct_mail">
        <fo:root>
            <fo:layout-master-set>
                <fo:simple-page-master master-name="order" page-width="210mm" page-height="297mm">
                    <fo:region-body margin="10mm"/>
                    <fo:region-before extent="10mm"/>
                    <fo:region-after extent="10mm"/>
                </fo:simple-page-master>
            </fo:layout-master-set>
            <fo:page-sequence master-reference="order">
                <fo:flow flow-name="xsl-region-body">
                    <fo:block font-size="11pt" font-family="Times">
                        <fo:table>
                            <fo:table-column column-width="3mm"/>
                            <fo:table-column column-width="13mm"/>
                            <fo:table-column column-width="176mm"/>
                            <fo:table-body>
                                <fo:table-row height="10mm">
                                    <fo:table-cell background-color="blue"/>
                                    <fo:table-cell background-color="blue"/>
                                    <fo:table-cell/>
                                </fo:table-row>
                                <fo:table-row height="28mm">
                                    <fo:table-cell background-color="blue"/>
                                    <fo:table-cell/>
                                    <fo:table-cell background-color="blue" text-align="center" display-align="center">
                                        <fo:block font-size="36pt" color="white" font-weight="bold" font-style="italic">
                                            <xsl:apply-templates select="sender"/>
                                        </fo:block>
                                    </fo:table-cell>
                                </fo:table-row>
                                <fo:table-row height="{297-20-10-28}mm">
                                    <fo:table-cell background-color="blue"/>
                                    <fo:table-cell background-color="blue"/>
                                    <fo:table-cell>
                                        <fo:block padding-top="20mm" margin-left="15mm">
                                            <xsl:apply-templates select="head"/>
                                            <xsl:apply-templates select="body"/>
                                            <xsl:apply-templates select="tail"/>
                                        </fo:block>
                                        <fo:block text-align="center" border-top="1pt solid black" font-size="8pt" font-style="italic" space-before="18mm" padding-top="1mm">
                                            This document is an example of direct mail and doesn't correspond to validity. All content is invented.
                                        </fo:block>
                                    </fo:table-cell>
                                </fo:table-row>
                            </fo:table-body>
                        </fo:table>
                    </fo:block>
                </fo:flow>
            </fo:page-sequence>
        </fo:root>
    </xsl:template>
    <xsl:template match="p | list">
        <fo:block space-after="7mm">
            <xsl:apply-templates/>
        </fo:block>
    </xsl:template>
    <xsl:template match="b">
        <fo:block font-weight="bold">
            <xsl:apply-templates/>
        </fo:block>
    </xsl:template>
    <xsl:template match="item">
        <fo:block>
            -&#xA0;&#xA0;&#xA0;<xsl:apply-templates/>
        </fo:block>
    </xsl:template>
    <xsl:template match="head">
        <fo:block space-after="20mm">
            <fo:block>
                <xsl:apply-templates select="date"/>
            </fo:block>
            <fo:block>
                <xsl:apply-templates select="address"/>
            </fo:block>            
            <fo:block>
                <xsl:apply-templates select="city"/><xsl:apply-templates select="country"/>
            </fo:block>
            <fo:block>
                <xsl:apply-templates select="phone"/>
            </fo:block>
        </fo:block>
    </xsl:template>
    <xsl:template match="recipient">
        <fo:block space-after="5mm">
            Dear <xsl:apply-templates/>,
        </fo:block>
    </xsl:template>
    <xsl:template match="tail">
        <fo:block space-before="5mm" space-after="5mm">
            <fo:block>
                <xsl:apply-templates select="politeness"/>
            </fo:block>
            <fo:block font-weight="bold">
                We are located at:
            </fo:block>
            <fo:block>
                <xsl:apply-templates select="contacts/address"/>
            </fo:block>
            <fo:block>
                <xsl:apply-templates select="contacts/city"/><xsl:apply-templates select="contacts/country"/>
            </fo:block>
            <fo:block>
                <xsl:apply-templates select="contacts/phone"/>
            </fo:block>
        </fo:block>
        <fo:block margin-left="60mm">
            <fo:block>
                Sincerely,
            </fo:block>
            <fo:block>
                <xsl:apply-templates select="signature/name"/>
            </fo:block>
            <fo:block>
                <xsl:value-of select="//sender"/>, <xsl:apply-templates select="signature/title"/>
            </fo:block>
        </fo:block>
    </xsl:template>
</xsl:stylesheet>
